Search code examples
avplayermpmediaitem

How to compare AVPlayer Cureent Item with MPMedia Item?


I am implementing an audio based application. In that I am using AVPlayer to play the list of MPMedia items selected from iPod Library. In my app I need to test 1 case, that is I need to compare the currently playing (from AVPlayer) with the list of MPMedia Items. How can I do this?

For easy understanding the following is I need:

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

        printf("Do some action");
    }
}

Solution

  • 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");
    }