Setting `currentPlaybackTime` fails after setting a new queue

Originator:joel
Number:rdar://46784008 Date Originated:December 17, 2018
Status:open Resolved:
Product:MusicKit on iOS Product Version:12.0-12.1
Classification: Reproducible:Yes
 
Summary:
We are heavily using the MediaPlayer framework, and we are often setting queues. One of the cases in which we set a queue is in shuffling. We do not use the standard MediaPlayer shuffle because we then don't have visibility into what the shuffled queue looks like, so we manually shuffle the queue and set the new (shuffled) queue.

A second use case also demonstrating this issue is attempting to add a new track to the end of the currently playing queue. 

However, when doing this, we are having an issue where the `currentPlaybackTime` is reset to 0 and the track starts playing from the beginning despite our attempts to move the playback time back to where the user was before she hit the shuffle or “add track to end of queue” button.

A forum thread talking about this issue is here: https://forums.developer.apple.com/thread/85602 and seems to suggest the issue was created in iOS 11.3.

We have attempted to solve this issue in both of the suggested ways. First, by using the `prepareToPlay` with completionBlock, which simply did not work. Second, by using a modified version of the infinite while loop solution where we cap the attempts to move the playback position at 300 over 1.5 seconds. This seems to work about half the time, but also usually causes undesirable hitching in the playback when it works, and when it doesn't work, the position is set to 0 anyway.

We can solve this problem by simply accepting that we have to have the playback position reset to 0 anytime we set a new queue, but we would prefer to allow the user to resume playback where they were if it is possible to do so.

Steps to Reproduce:
1. Using the MediaPlayer framework, set a new queue of items.
2. When the `MPMusicPlayerControllerNowPlayingItemDidChange` notification comes through, attempt to change the playbackPosition of the new now playing track to some time in the track's length.
3. It will fail (9/10 times, in our experience) and the playback time will remain at 0.

Using code such as the following, the failure rate drops, but does not disappear:

```
pauseAudio()
// HAX: A new queue has been set. Set the saved playback position when the track starts playing
DispatchQueue.global().async { [unowned self] in
var index = 0
while true {
usleep(5000)
if index > 300 {
self.playbackPositionManager.updatePlaybackPosition(0)
self.playAudio()
break
}

if self.musicPlayerController.currentPlaybackTime != newPosition {
self.musicPlayerController.currentPlaybackTime = newPosition
self.playAudio()
break
}

index += 1
}
}
```

Expected Results:
The `currentPlaybackPosition` should successfully change the location of the new currently playing track

Actual Results:
The `currentPlaybackPosition` silently fails change the location of the new currently playing track for a significant period of time

Version/Build:
iOS 12.0 - 12.1

Configuration:

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!