Question

My app uses an MPMusicPlayerController to play music. I'm getting a bit lost in all the documentation for it and MPMediaQuery and so on.

What I want to do is play a particular album, beginning at a specific track.

I can construct a MPMediaQuery that reruns the album, but how do I start playback at the specific track?

Was it helpful?

Solution

Well, if you have the query, I'm assuming you already have a MPMediaItemCollection that you'd like to play. From there, you assign that collection as the player's queue, and a specific MPMediaItem from the collection as the player's now playing item. The collection declares an items property that allows you to access the individual media items by index, which you can use to assign the now playing item. In this case 0 will play the first track in the collection, but this can be set to any index within the array's bounds.

MPMusicPlayerController *controller = [MPMusicPlayerController iPodMusicPlayer];

[controller setQueueWithItemCollection:myMediaItemCollection];
[controller setNowPlayingItem:myMediaItemCollection.items[0]];

[controller prepareToPlay];
[controller play];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top