Play composition at specific index of MPMediaItemCollection in Swift

I am trying to make my own Musicplayer with Swift. I need to go to a specific song / index of my MPMediaItemCollection and start playing, but I can only find methods like skipToNextItem () and skipToPreviousItem (). Is there another way to do this than with a loop?

let player = MPMusicPlayerController.systemMusicPlayer()
player.setQueueWithItemCollection(mediaCollection)
player.play()

      

+3


source to share


1 answer


As per the documentation, we are using the property nowPlayingItem

.

To specify that playback should start from a specific media object in the play queue, set this property to that element when the music player is stopped or paused.



So it looks like you have to stop or pause the player, install nowPlayingItem

and then call again play

.

player.nowPlayingItem = mediaCollection.items[selectedIndex]
player.play()

      

+4


source







All Articles