2011-09-07 33 views
0

我正在實現一個基於音頻的應用程序。我正在使用AVPlayer播放從iPod庫中選擇的MPMedia項目列表。在我的應用程序中,我需要測試1個案例,那就是我需要將當前正在播放的(來自AVPlayer)與MPMedia項目列表進行比較。我怎樣才能做到這一點?爲了便於理解,以下是我需要的,如何比較AVPlayer Cureent物品和MPMedia物品?

for(MPMediaItems) 
{ 
    if([MPmedia Item]== [AVPlayer CurrentItem]) 
    { 

     printf("Do some action"); 
    } 
} 

你們能幫我解決嗎?這對我來說非常緊迫。

由於提前, Sekhar

+0

我用下面的代碼試過,但不能成功,MPMediaItem * anItem = [歌曲objectAtIndex:指數]; NSURL * itemURL = [anItem valueForProperty:MPMediaItemPropertyAssetURL]; AVAsset * asset = [[AVURLAsset alloc] initWithURL:itemURL options:nil]; NSString * tracksKey = @「tracks」; [asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:tracksKey] completionHandler:^ {AVPlayerItem * item = [AVPlayerItem playerItemWithURL:itemURL]; AVPlayerItem * nextPlayItem = [AVPlayerItem playerItemWithAsset:asset]; if(nextPlayItem == [appDelegate.avPlayer currentItem]) { } }]; – ChandraSekhar

回答

3
MPMediaItem *song; 
NSURL *songURL = [song valueForProperty: MPMediaItemPropertyAssetURL]; 
AVURLAsset *asset1 = (AVURLAsset *)[_avPlayer.currentItem asset]; //currentItem is AVAsset type so incompitable pointer types ... notification will occur, however it does contain URL (see with NSLog) 
AVURLAsset *asset2 = [AVURLAsset URLAssetWithURL: songURL options: nil]; 
if ([asset1.URL isEqual: asset2.URL]) { 
    printf("Do some action"); 
}