views:

159

answers:

2

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

A: 

There is no way around this. If the users iPod app is playing an iPod selection, then all remote events are going to go to the iPod, not your app.

coneybeare
I know that. Again, that's not the question. The only time the app is playing is when you use the MPMusicPlayer with ipodMusicPlayer. The applicationMusicPlayer is an instance in your app. But even if that's the case, that still doesn't explain why when you're using an AVAudioPlayer, which is definitely not related to the app, you can't use the asset's URL from the MPMediaItem which the docs explicitly state is for that purpose.Again, I am trying to let a user play a file from the library. It doesn't have to be with the iPod app.
MarqueIV
Seems it is possible! Check this out... http://www.engadget.com/2010/07/08/ios-4-adds-direct-access-to-itunes-library-iphone-dj-apps-about/
MarqueIV
A: 

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!)

MarqueIV