Search code examples
iosswiftmedia-player

skip to item of index number... on iOS Media Player with Swift


I would like to skip to another song using the index number of an item (that you can find with playlist.indexOfNowPlayingItem). Is there an easy way to do that with a function like mediaplayer.skipToNextItem but setting the index number ( for example mediaplayer.skipToItem(4) if i want to skip to the fourth item of my playlist) ?


Solution

  • The solution is, creating a collection as MPMediaItemCollection, to set the song to play with : player.nowPlayingItem = collection.items[1]

    for example :

    var query = MPMediaQuery.songsQuery()
    var collection = MPMediaItemCollection(items: query.items!) //it needs the "!"
    let player = MPMusicPlayerController.applicationMusicPlayer()
    
    player.setQueueWithItemCollection(collection)
    
    player.nowPlayingItem = collection.items[1]
    

    Note : Ifound the answer here : Play Song at specific index of MPMediaItemCollection in Swift