Home > Software design >  Background Audio mode with MPMusicPlayerController.systemMusicPlayer
Background Audio mode with MPMusicPlayerController.systemMusicPlayer

Time:08-24

I'm working on an iOS app that uses MPMusicPlayerController.systemMusicPlayer to play songs from Apple Music to the user.

My app should be able to append songs to the Music player's queue based on messages it receives from a server. I have this working with a simple WebSocket connection between the app and the server, but as soon as the app enters the background the socket is automatically closed (which makes sense).

Because the actual music playback is done by the Music app, I can't use the Background Audio background mode to keep my app alive. Is there a way around this?

Things I have already considered (and why I don't think they will work):

  • Remote Notifications are throttled too slow to be of any real use
  • Background App Refresh is also too slow
  • PushKit / VoIP (the app isn't a VoIP app)
  • Playing "blank" or nearly silent audio over the actual audio which seems too "hacky" and likely won't pass app review
  • Using background location tracking (again, almost certainly won't pass review)
  • Ditching systemMusicPlayer completely and using AVAudioPlayer with the Apple Music API (this would be reinventing the wheel a little bit and would force streaming even if the media was downloaded)
  • Using applicationQueuePlayer and just forcing the user to stay in app (this would be a bad user experience imo, they should be able to listen in the background)

Any help would be appreciated, thank you!

CodePudding user response:

In iOS 15 and later, you should be able to just set the background plist flag for your app, and then just use ApplicationMusicPlayer.

https://developer.apple.com/documentation/musickit/applicationmusicplayer

  • Related