0 Replies
      Latest reply on Nov 7, 2019 6:15 PM by hbhbjhjhb
      hbhbjhjhb Level 1 Level 1 (0 points)

        I've been having trouble with the Media Player Framework and UICollectionView.



        It all starts by invoking my function `setPlayerQueue(with storeIds: [String], start index: Int)` (see below) from the `didSelectItemAt` UICollectionViewDelegate method. This is expected to enqueue the store ids in an order that matches the display of tracks in the UICollectionView. So if cell 15 is tapped, track 15 should play. What actually happens is that track 1 plays first, and if I tap again, then track 15 will play as expected.



        When I tap a cell in my UICollectionView, a queue is created using an array of store ids via the [`func setQueue(with descriptor: MPMusicPlayerQueueDescriptor)`](https://developer.apple.com/documentation/mediaplayer/mpmusicplayercontroller/2582423-setqueue). I also assign the MPMusicPlayerQueueDescriptor `startItemID` property to the store id associated with the selected index path before invoking `setQueue(descriptor)`.




        func setPlayerQueue(with storeIds: [String], start index: Int) {



          let descriptor: MPMusicPlayerStoreQueueDescriptor = MPMusicPlayerStoreQueueDescriptor(storeIDs: storeIds)



          descriptor.startItemID = storeIds[index]



          player.setQueue(with: descriptor) // player: MPMusicPlayerController.applicationMusicPlayer








        1. Create array of store ids (e.g. 20 tracks)

        2. Scroll to bottom of UICollectionView (e.g. 20 items)

        3. Call `setPlayerQueue(with: storeIds, start: indexPath.item)` by selecting any cell (e.g. index 17)

        4. Call `prepareToPlay() { e: Error? in }`, followed by `play()` in its closure



        I expect track 17 to play, but track 1 plays instead. If I tap the same cell 17 again, track 17 will play as expected.



        Any help will be appreciated.