ApplicationMusicPlayer.shared.state.playbackStatus unexpected results

Hi -

Of course I may be doing something wrong, but I'm getting exactly the opposite of what I would expect from

ApplicationMusicPlayer.shared.state.playbackStatus

It returns .playing when the music is paused and .paused when the music is playing.

Am I holding it wrong?

Thanks,

Daniel

Hello @dimsumthinking,

The main thing to know about MusicPlayer.State is that it conforms to ObservableObject, thus making it very easy to use with SwiftUI. The primary way that it's designed to be used is in a SwiftUI View, using ObservedObject.

Please refer to our official sample app to see an example of how this can be used: Using MusicKit to Integrate with Apple Music

It would definitely help for you to share a bit more about how you're trying to use ApplicationMusicPlayer.shared.state.playbackStatus. Using that outside of a SwiftUI View is possible, but it might require special care to catch object changes at the right time.

I hope this helps.

Best regards,

Since you've solicited info:

I just encountered a problem with MusicPlayer.State, in that it doesn't work with the current Observation framework. For example, this does not work:

	withObservationTracking {
		_ = musicPlayer.state.playbackStatus
	} onChange: {
		Task
		{
			print("PlaybackManager observed player-state change, to \(await self.musicPlayer.state).")
			await self.playerChanged()
			await self.startObserving()	// await because of MainActor
		}
	}

I had migrated my application's observation usage to the new world order, but it looks like I have to revert files that use MusicPlayer.

Also: I urge the frameworks developers to abandon the idea that people are just slapping stuff into a SwiftUI view and calling it a day. This has turned development on Apple platforms into a tedious slog of workarounds and tricks to let controller-type objects communicate with each other; and repeated disappointment when each new observation regime repeats the same mistakes (for example, only offering "willChange" and not "didChange").

ApplicationMusicPlayer.shared.state.playbackStatus unexpected results
 
 
Q