MPMusicPlayerController knows the previous and next song

MPMusicPlayerController gives me the nowPlayingItem property, but I'm also interested in the previous next song.

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

+4
source share
2 answers

How do you set the play queue for your MPMusicPlayerController? Perhaps you are using MPMediaQuery to search for MPMediaItemCollection, which is then set as the MPMusicPlayerController queue.

If so, you can take the MPMusicPlayerController indexOfNowPlayingItem property in iOS 5+, add or subtract it, and select MPMediaItem from the items property MPMediaItemCollection that you found. (On iOS <5, you can get the nowPlayingItem index in the items array and use this instead of indexOfNowPlayingItem.)

The fact is that you cannot access the items in the player queue, but if you save the collection of items that you originally assigned to the queue, you can access the items in this collection.

+1
source

You can play the next and previous songs this way:

Swift 2.0

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

100% verified code is executed.

+1
source

Source: https://habr.com/ru/post/1379553/


All Articles