I have a player control in the app the controls system music player (Prev Play/Pause Next).
To show proper Play/Pause state I'm adding an observer to MPMusicPlayerControllerPlaybackStateDidChange notification.
Everything goes smooth while Apple Music App is running. But if I close the Apple Music app and press play button in my app that triggers the playPause() function the app gets crashed right after I tried to get current playbackState while handling new MPMusicPlayerControllerPlaybackStateDidChange notification.
func playPause() {
let isPlaying = MPMusicPlayerController.systemMusicPlayer.playbackState == .playing
isPlaying ? controller.pause() : controller.play()
}
The code that handles that notification:
@objc
private func onPlayBackStateChanged() {
let playbackState = MPMusicPlayerController.systemMusicPlayer.playbackState // <<< This line crashes the app
updatePlaybackState(playbackState)
}
Right before crash I'm receiving this log:
2020-03-25 13:46:20.471063+0200 MYAPP[405:14911] [SDKPlayback] prepareToPlay failed [no target descriptor]
2020-03-25 13:46:33.305925+0200 MYAPP[405:14911] [SDKPlayback] prepareToPlay failed [no target descriptor]
2020-03-25 13:46:41.498197+0200 MYAPP[405:15072] [SDKPlayback] systemMusicPlayer connection invalidated
This code works well with iOS <13.3x but it doesn't work with iOS 13.4. Is this new bug from Apple or am I doing something wrong? How to fix this bug?
I’m having the exact same issue - feature worked fine in iOS 13.3 but broke in 13.4. I filed a bug with Apple through Feedback Assistant. They were able to recreate the issue and there is a fix in iOS 13.5 beta 4 (the fix worked for me).
Should be fixed in the release version of 13.5.