How to addObserver for currentPlaybackTime for the system music player?

I'm using the systemMusicPlayer to play music and want to update the playback time using addObserver forKeyPath.

[self setMusicPlayer: [MPMusicPlayerController systemMusicPlayer]];

I've tried these two methods:

[self addObserver:self forKeyPath:@"musicPlayer.currentPlaybackTime" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial context:&musicPlayer]; [self.musicPlayer addObserver:self forKeyPath:@"currentPlaybackTime" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial context:&musicPlayer];

I do get the initial values for currentPlaybackTime in: -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

but I never get any calls when the player is playing the song (the whole point).

If I set the currentPlaybackTime to a specific value (locating manually using a slider), I get calls with the values I set (useless since I know what I am setting them to).

How are we supposed to track the playback time without just polling it constantly?

you can register for notifications of the player for events such as starting, stopping, pausing, etc. While the music player is playing, you'll want to set up a Timer to periodically update a progress bar. In SwiftUI you can use the TimelineView to periodically update the progress bar.

https://developer.apple.com/documentation/swiftui/timelineview

How to addObserver for currentPlaybackTime for the system music player?
 
 
Q