1

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:

  • I'm using MPMusicPlayerController.systemMusicPlayer to access information about the currently playing music item on the Music.app.

  • On iOS devices, nowPlayingItem correctly returns the metadata of the currently playing music item.

  • However, when running the same code on MacCatalyst, nowPlayingItem consistently returns nil, even though playbackState indicates that music is playing (playbackState == .playing).

  • I've verified that my app has the necessary authorization to access the user's media library, and there are no errors reported during authorization.

  • This issue occurs consistently across different MacCatalyst environments and OS versions.

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!

1
  • Pierre, I tried your code. when Apple Music is playing on macOS, the playbackState's raw value is 0, which is stopped. Besides, there is a log in xcode console: systemMusicPlayer _establishConnectionIfNeeded failed [application failed to launch]. I think that means the XPC connection has failed in macCatalyst. Commented May 19, 2024 at 22:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.