Get queue and current item from ApplicationMusicPlayer?

I'd like to be able to monitor what is queued and what is currently playing in MusicKit's ApplicationMusicPlayer.

Use cases:

  • Show a "playing" indicator on the current track in a list.
  • Show a "now playing" bar with song title, album, and artist.

Is this possible?

Hi @Alex at Astrocode,

Thanks for your feedback about MusicKit's ApplicationMusicPlayer.

What you're looking for is not currently possible with the existing seeds, but we are working on this very thing right now.

Stay tuned.

Best regards,

FB9155477

Hello @Alex at Astrocode,

We have made a number of changes to MusicKit's playback API in iOS 15 beta 4.

You can now access ApplicationMusicPlayer.shared.queue to monitor the queue. This object is observable, so you can even use it with @ObservedObject in your SwiftUI views.

You can also get information about what is currently playing with ApplicationMusicPlayer.shared.queue.currentEntry.

I hope this helps.

Best regards,

This is how I did it in a class, not in a view:

var queueObserver: AnyCancellable?

 let musicPlayer = ApplicationMusicPlayer.shared
        
        if queueObserver == nil {
            queueObserver = musicPlayer.queue.objectWillChange
                .sink { [weak self] in
                    self?.queueDidChange()
                }
        }
        
        return musicPlayer
}

private func queueDidChange() {
        // When we get this event, the song hasn't actually changed yet so we try a little later.
        DispatchQueue.main.asyncAfter(deadline: .now() + 2.0, execute: {
            NowPlaying.shared.setSong(self.musicPlayer.queue.currentEntry?.title ?? "Unknown")
        })
}
Get queue and current item from ApplicationMusicPlayer?
 
 
Q