Play Song at specific index of MPMediaItemCollection in Swift

我的未来我决定 提交于 2019-12-10 15:49:11

问题


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()

回答1:


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()


来源:https://stackoverflow.com/questions/31292774/play-song-at-specific-index-of-mpmediaitemcollection-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!