Luca Bernardi
Luca Bernardi

Reputation: 4199

MPMusicPlayerController know the previous and next song

MPMusicPlayerController gives me the property nowPlayingItem, but i'm also interestend in the previous e next song.

Is there any way to retrive a MPMediaItem with the next and previous elements?

Upvotes: 4

Views: 1923

Answers (2)

zzzz
zzzz

Reputation: 58

You can play next and Previous songs in this way:

Swift 2.0

let musicPlayer: MPMusicPlayerController?
musicPlayer.skipToNextItem()  // Next Song
musicPlayer.skipToPreviousItem() // Prev Song
musicPlayer.skipToBeginning() // Start Of Current Media

Works 100% tested Code.

Upvotes: 1

arlomedia
arlomedia

Reputation: 9071

How are you setting the playback queue for your MPMusicPlayerController? You might be using an MPMediaQuery to find an MPMediaItemCollection, which you then set as the MPMusicPlayerController's queue.

If that's the case, you can take the MPMusicPlayerController's indexOfNowPlayingItem property in iOS 5+, add or subtract one, and select an MPMediaItem out of the items property of the MPMediaItemCollection that you found. (In iOS < 5, you can get the index of nowPlayingItem in the items array and use that in place of indexOfNowPlayingItem.)

The point is that you can't access the items in a player's queue, but if you keep the collection of items that you originally assigned to the queue, you can access the items in that collection.

Upvotes: 1

Related Questions