0

I am loading my iPod library into AVQueuePlayer and playing it using this:

[[AVQueuePlayer alloc]]initWithItems:[MPMediaCollectionInstance items] ];  //just one line.

But how do I read which MPMediaItem is currently playing? I want to know information like artist / song name etc. Thanks.

Z S
  • 7,039
  • 12
  • 53
  • 105

1 Answers1

2

Have the instance of the AVQueuePlayer that you have allocated.

AVQueuePlayer *_queuePlayer = [[AVQueuePlayer alloc] initWithItems:[MPMediaCollectionInstance items]];

With that instance, you can get the AVPlayerItem.

AVPlayerItem *currentItem = _queuePlayer.currentItem; 

For the above line, please check the doc reference.

And now try the following code

NSArray *metadataList = [currentItem.asset commonMetadata];
for (AVMetadataItem *metaItem in metadataList) {
    NSLog(@"%@",[metaItem commonKey]);
}

Which will give a list as follows:

title
creationDate
artwork
albumName
artist

Now you can get the value for corresponding keys. For this, you have to refer this doc too.

Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
  • I only seem to get an empty array for metadataList. When do you call the code to get the metadata? Right after playing the queue? – Z S Nov 21 '11 at 10:10
  • 1
    Please check whether you have valid `_queuePlayer` and `currentItem` objects. Yes, we need to call that after playing the queue. – Ilanchezhian Nov 21 '11 at 10:18
  • It's still coming empty. I'm running this on an iOS5 device. I tried this right after initializing the queue and calling play. I also tried this in the KVO notification sent to the playerItem that was enqueued ... both cases, commonMetadata returns an empty array. – Z S Nov 21 '11 at 10:37
  • Apparently, this works: NSArray *metadataListPlaying = [currentItem.asset metadataForFormat:@"com.apple.itunes"]; this gives an array of 8 keys. Not sure what the difference is, and what kind of formats I should be using if I'm picking songs from the iPod library. – Z S Nov 21 '11 at 11:02