Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.) in my app so I can do some extra things. So far I can get either iPod music playing, or headphone events, but not both simultaneously.

Here's what I know so far...

  1. If you use the MPMusicPlayer, you can easily have programmatic access to the entire music library. However, it, not your app, receives the remote notifications regardless if you use applicationMusicPlayer or ipodMusicPlayer.

  2. If you use AVAudioPlayer (Apple's recommended player for most sounds in your app), you can easily get remote notifications, but it doesn't natively have access to the iPod library.

  3. AVAudioPlayer can be initialized with an asset URL, and tracks in the iPod library (type MPMediaItem) do have a URL property that returns a NSURL instance which the documentation says its explicitly for use with AVAsset objects, but when you try initializing the AVAudioPlayer with that NSURL, it fails. (I used the 'now playing' track in iPod which was a MP3 and it did return a valid NSURL object but initialization failed. Even worse, when it was an Audible.com file, the NSURL property flat-out returned nil.)

  4. If you try using an instance of the AVAudioPlayer to get remote events (say, with a blank sound file), then simultaneously use the MPMusicPlayer class to play iPod music, you have remote control access until you actually start iPod playback at which time you lose it since your audio session gets deactivated and the system audio session becomes active.

  5. If you try the same as #4 but you instead set the audio session's category to a mixable variant, your session doesn't get deactivated, but you still lose remote control capability once the iPod starts playing.

In short, whenever MPMusicPlayer is playing, I can't seem to get remote events, and I don't know of any other way to play content from the iPod's library other than by using MPMusicPlayer.

ANY suggestions on how to get around this would be welcome. Creative or flat-out crazy. Don't care so long as it works.

Anyone? Anyone? Bueller? Bueller?

M

like image 861
Mark A. Donohoe Avatar asked Jul 07 '10 02:07

Mark A. Donohoe


People also ask

Can you remote control Apple music?

iTunes Remote is the best way to control Apple Music, iTunes, or the Apple TV app from anywhere in your home. Simply download the app to your iPhone or iPad, and connect directly to Apple Music, iTunes, or the Apple TV app on your Mac or PC.

Is there a remote control for iPhone?

The TeamViewer remote control app* for iPhone and iPad allows you to access other devices using your iOS device when on the go: Access Windows, Mac and Linux PCs. Easily access computers and servers remotely using an iPhone or iPad. Access and transfer saved documents.

Can I control Apple music on my iPad from my iPhone?

Select Device. Select Use Other Device. Select the device that you want to control, then select Connect. Now you can use the switch connected to your iPhone, iPad, or iPod touch to control the other device.

Can I control music on my Mac from my iPhone?

You can use the iTunes Remote app on your iPhone, iPad, or iPod touch to control media libraries that are on your Mac and other computers. You can download the iTunes Remote app for free from the App Store on your iPhone or iPad.


2 Answers

HA! Solved! I knew it was possible! (Thanks Async-games.com support!)

Here's how to play iPod music in your app, with background support, and with your app receiving the remote control notifications.

You have to use AVPlayer (but not AVAudioPlayer. No idea why that is!) initialized with the asset URL from the MPMediaItem you got from the library picker (or current item in the MPMusicPlayerController or wherever), then set the audio session's category to Playable (do NOT enable the mixing override or you'll lose the remote events!) and add the appropriate keys to your info.plist file telling the OS your app wants to support background audio.

Done and done!

This lets you play items from your iPod library (except Audible.com files for some reason!) in the background and still get remote events. Granted since this is your audio player which is separate from, and will interrupt the iPod app, you have to do more work, but those are the breaks!

Damn though... I just wished it worked with Audible.com files. (For those interested, the reason it doesn't is the asset URL for an audible file returns nil. Stinks! But what can ya do!)

like image 139
Mark A. Donohoe Avatar answered Oct 13 '22 20:10

Mark A. Donohoe


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...

like image 29
Blaise Avatar answered Oct 13 '22 20:10

Blaise