Ega Setya Putra
Ega Setya Putra

Reputation: 1695

ios - How to play MPMediaItemCollection?

So, I already stored few mpmediaitemcollection in NSUserDefaults. I already retrieve it on UICollectionView.

here is my code:

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

}

and here is the decodedPlaylisData array:

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

above code supposed to play each mediaitemcollection according indexPath.row

getSongListInfo is a function that get all info such as image,title,album,artist and I send it to a struct. And it works like below on another vc:

enter image description here

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

How can I fix this problem?

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

Upvotes: 0

Views: 662

Answers (1)

Gene De Lisa
Gene De Lisa

Reputation: 3838

This code creates three separate music player controllers. Make one and store it in a variable, then send the messages to it

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

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

MPMusicPlayerController().play()

Upvotes: 0

Related Questions