How can you play music from the iPod app while still receiving remote control events in your app?

后端 未结 4 1456
一生所求
一生所求 2020-12-05 12:09

Ok, I\'m trying to let a user choose songs from their iPod library to listen to, but I still want to receive remote control notifications (headphones, lock screen osd, etc.)

4条回答
  •  日久生厌
    2020-12-05 13:00

    This is probably not going to be of any use anymore for the OP, but as it may be for people finding this page through googling, I will post it anyway.

    An alternative (but rather ugly) approach, if you are only interested in the music remote control events and still want to be able to play the audible.com files...

    Just keep using the MPMusicPlayer and track its notifications (now playing and state changed). To keep receiving these notifications in the background, you can do the "background thread magic" described in various places to keep your app from being suspended. You are not going to receive the remote controls directly (as the iPod player is receiving them), but by tracking the changes in "now playing" you can infer the ControlPreviousTrack and ControlNextTrack events, and by tracking the playbackState, you can infer the TogglePlayPause command.

    The downside is that you are app is going to be running at all times for no good reason (although, to be fair, if iOS is programmed correctly, a background thread doing nothing should consume almost no battery).

    Another alternative: use a MPMoviePlayer? I have checked that it works fine in the background, and should receive remote control events as well. It can play MPMediaItem natively, so hopefully the Audible.com files as well...

提交回复
热议问题