How to trigger music playback in Widget

Hello, I have a music player application that uses MPMusicPlayerController to play Apple Music songs. Now I want to add a Widget to it that can trigger playback pause on iOS 17. How do I need to achieve this? I can get the playbackStoreID of the song on the Widget Extension and use MPMusicPlayerController setQueue(with:) to play it. How do I pass the playbackStoreID in the Widget to the main application for playback, or should I create a new MPMusicPlayerController on the Widget Extension for playback?

Replies

Your Widget should call an App Intent, via button or toggle which can start playback (even in the background) of your audio. Some additional things to consider:

  1. If your app intent is compiled only into your Widget Extension, it will only run in your Widget Extension.
  2. If your app intent is also compiled into your app, it will run in your app if your app is currently running and not suspended.
  3. If you want your app intent to always run in your app, you can:
  • implement openAppWhenRun, in which case it'll foreground your app OR
  • implement AudioIntent, LiveActivityIntent or ForegroundContinuableIntent in which case it'll run in your app, but run in the background.