2

I'm experiencing an issue with the MPMusicPlayerController.applicationQueuePlayer when setting the queue using an MPMusicPlayerMediaItemQueueDescriptor with specific start and end times. Instead of updating to the new start and end times, the applicationQueuePlayer reverts to its last known configuration for the previously used MPMediaItem.

Implementation details:

Users pick songs from their Apple Music library. In the code, the song is set to the MPMusicPlayerController.applicationQueuePlayer using the MPMusicPlayerMediaItemQueueDescriptor. The start and end times are set immediately. However, since the update to version 16.4, setting the start and end times using MPMusicPlayerMediaItemQueueDescriptor seems to be broken if you set it to a previously set MediaItem. After passing the TimeInterval, the start and end times appear as "unknown" the second time.

Example:

User chooses a song. App sets the song to the queue player with a start time of 30 seconds and end time of 60 seconds. User plays that segment of the song (works correctly). Song ends, stop is called on the applicationQueuePlayer (should clear the queue). User changes the start segment of the song to 20 seconds. App sets the song to the queue player with a start time of 20 seconds and end time of 60 seconds. User hits play, but step #2 plays instead. Steps to reproduce:

Create a static reference to the applicationQueuePlayer: swift Copy code

static var musicPlayer: MPMusicPlayerController = {
    MPMusicPlayerController.applicationQueuePlayer
}()

Grab a song from the Apple Music Library containing a persistentId. Ensure it's added to your Apple Music Library and downloaded to the device. In the mediaPicker delegate callback, assign the MPMediaItem retrieved to a local variable. Use the following function to create a descriptor:

static func descriptor(fromMediaItem mediaItem: MPMediaItem,
                       startTime: TimeInterval,
                       endTime: TimeInterval) -> MPMusicPlayerMediaItemQueueDescriptor {
    let mediaCollection = MPMediaItemCollection(items: [mediaItem])
    let descriptor = MPMusicPlayerMediaItemQueueDescriptor(itemCollection: mediaCollection)
    descriptor.setStartTime(startTime, for: mediaItem)
    descriptor.setEndTime(endTime, for: mediaItem)
    descriptor.startItem = mediaItem
    return descriptor
}

Set the descriptor to your applicationQueuePlayer using setQueue(with: descriptor).

Is there a known workaround or solution for this issue?

Joshua Hart
  • 772
  • 1
  • 21
  • 31
  • If behavior that worked has broken in 16.4, file a bug. – matt Apr 08 '23 at 23:56
  • before calling ```descriptor``` try calling ```musicPlayer.stop()```, ```musicPlayer.setQueue(with: [])``` and ```musicPlayer.nowPlayingItem = nil``` – udi Apr 19 '23 at 04:55

0 Answers0