Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Song at specific index of MPMediaItemCollection in Swift

I try to make my own Musicplayer with Swift. I need to jump to a specific song/index of my MPMediaItemCollection and start to play it but i can only find methods like skipToNextItem() and skipToPreviousItem(). Is there any other way to do that than with a loop?

let player = MPMusicPlayerController.systemMusicPlayer()
player.setQueueWithItemCollection(mediaCollection)
player.play()
like image 236
user2942012 Avatar asked Jul 08 '15 12:07

user2942012


1 Answers

According to the documentation, we use the nowPlayingItem property.

To specify that playback should begin at a particular media item in the playback queue, set this property to that item while the music player is stopped or paused.

So, it sounds like you should stop or pause the player, set the nowPlayingItem, and then call play again.

player.nowPlayingItem = mediaCollection.items[selectedIndex]
player.play()
like image 191
nhgrif Avatar answered Oct 20 '22 01:10

nhgrif