Pergunta

Im trying to retrieve the track number of the songs in my iPod library.

Basically I've queried for all the albums as follows:

NSArray *albums = [MPMediaQuery albumsQuery].collections;

Retrieved the an album:

MPMediaItemCollection *album = self.albums[0];

Iterate through the songs and try to log the track number

for (MPMediaItem *mediaItem in self.album.items)
{
    NSLog(@"Track Number %@", [mediaItem valueForKey:MPMediaItemPropertyAlbumTrackCount]);
}

But this gives me the following error

Terminating app due to uncaught exception 'NSUnknownKeyException', reason:
'[<MPConcreteMediaItem 0x1cd818d0> valueForUndefinedKey:]: this class is 
not key value coding-compliant for the key albumTrackCount.'

If I try other keys such as MPMediaItemPropertyTitle, MPMediaItemPropertyAlbumTitle or MPMediaItemPropertyArtist works fine. Is there something I'm missing as to why I can't access the MPMediaItemPropertyAlbumTrackCount.

Foi útil?

Solução

You used valueForKey: that's why the error happening. Use valueForProperty: like:

NSLog(@"Track Number %@", [mediaItem valueForProperty:MPMediaItemPropertyAlbumTrackCount]);

In the MPMediaItem Class Referece Apple says that:

You obtain metadata for a media item by calling the valueForProperty method with these property keys.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top