Pierre Janineh
Pierre Janineh

Reputation: 702

MPMusicPlayerController.systemMusicPlayer.nowPlayingItem returns nil on MacCatalyst while playbackState is .playing

I'm encountering an issue with MPMusicPlayerController.systemMusicPlayer.nowPlayingItem on MacCatalyst, where it returns nil even though playbackState reports .playing.

This behavior differs from iOS, where the expected music item is returned.

Here's a summary of the problem:

Here's the relevant code snippet:

import MediaPlayer

MPMediaLibrary.requestAuthorization { status in
    if status == .authorized {
        if let nowPlayingItem = MPMusicPlayerController.systemMusicPlayer.nowPlayingItem {
            // This block is never executed on MacCatalyst
            let title = nowPlayingItem.title ?? "Unknown Title"
            let artist = nowPlayingItem.artist ?? "Unknown Artist"
            print("Currently Playing: \(title) by \(artist)")
        } else {
            // This block is always executed on MacCatalyst
            print("No music is currently playing.")
        }
    } else {
        print("Authorization denied.")
    }
}

I've searched through Apple's documentation and developer forums, but I haven't found any specific information about this issue or any known workarounds.

Any insights or suggestions on why nowPlayingItem returns nil on MacCatalyst while the playback state is .playing, and how to resolve or work around this issue, would be greatly appreciated. Thank you!

Upvotes: 1

Views: 97

Answers (0)

Related Questions