ios - 如何玩MPMediaItemCollection?

ios - How to play MPMediaItemCollection?

所以,我已经在 NSUserDefaults 中存储了一些 mpmediaitemcollection。我已经在 UICollectionView 上检索了它。

这是我的代码:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    self.performSegueWithIdentifier("toList", sender: self)

    User.mdci = decodedPlaylistData[indexPath.row] as? MPMediaItemCollection

    getSongListInfo((decodedPlaylistData[indexPath.row] as? MPMediaItemCollection)!)

    MPMusicPlayerController().setQueueWithItemCollection((decodedPlaylistData[indexPath.row] as? MPMediaItemCollection)!)
    MPMusicPlayerController().nowPlayingItem = (decodedPlaylistData[indexPath.row] as? MPMediaItemCollection)!.items[0] as! MPMediaItem

    MPMusicPlayerController().play()

}

这是解码后的 PlaylisData 数组:

(
"<MPMediaItemCollection: 0x165c5d00>",
"<MPMediaItemCollection: 0x165c6c30>",
"<MPMediaItemCollection: 0x165c7390>",
"<MPMediaItemCollection: 0x165c7c60>"
)

以上代码应该根据 indexPath.row

播放每个 mediaitemcollection

getSongListInfo 是一个获取所有信息(例如图像、标题、专辑、艺术家)的函数,我将其发送到结构。它在另一个 vc:

上的工作原理如下

I can get song info from MPMediaItemCollection, but I can't play the song from MPMediaItemCollection

我该如何解决这个问题?

note: It actually play the MPMediaItemCollection, But when I clicked another UICollectionViewCell it's not playing

此代码创建三个独立的音乐播放器控制器。制作一个并将其存储在一个变量中,然后将消息发送给它

MPMusicPlayerController().setQueueWithItemCollection((decodedPlaylistData[indexPath.row] as? MPMediaItemCollection)!)

MPMusicPlayerController().nowPlayingItem = (decodedPlaylistData[indexPath.row] as? MPMediaItemCollection)!.items[0] as! MPMediaItem

MPMusicPlayerController().play()