Question

I am trying to initialize an AVPlayer with the contents of a song from the device iPod library. I have the following code in a viewDidLoad of my root controller (extracted it into a test app to isolate it).

MPMediaQuery* query = [MPMediaQuery songsQuery];

MPMediaItem* firstItem = [query.items objectAtIndex:0];
NSURL* url = [firstItem valueForKey:MPMediaItemPropertyAssetURL];
AVPlayerItem* playerItem = [[AVPlayerItem alloc] initWithURL:url];

self.player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
[self.player play];

The code always throws the following exception at the line where I call valueForKey on firstItem in order to obtain the asset URL.:

* Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key assetURL.' * First throw call stack:

The code seems pretty straight forward to me, not sure what I might be missing here, any help would be fantastic. This is iOS 5.1 on an iPhone 4S. Tried with both iTunes Match enabled and with a device that only has local music, same result.

Was it helpful?

Solution

You need to use valueForProperty: method instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top