9

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

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286

4 Answers4

16

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

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
  • Marquel, any idea why the Audible.com files are broken this way? – Genericrich May 24 '11 at 14:54
  • My guess is that they aren't broken, but rather this is the way Audible makes it so you can't write an 'Audible book ripping' app, but that's purely a guess. I never found out the official reason. Stinks too because it killed my product. Let me know if *you* ever find the answer. (Also, vote this question up if you like it.) – Mark A. Donohoe May 25 '11 at 04:59
  • 2
    Wow. Thanks for this incredibly detailed explanation written quite well. And organized beautifully. I'm playing the user's audio files in a casual music game in response to their choice from a prompt. I too was using the `[MPMusicPlayerController iPodMusicPlayer]` to play audio files but realized how cheesy that was. I tried to figure out how to enable backgrounding for the `applicationMusicPlayer` but couldn't figure it out. This provided 100% of the answer I needed. You just saved me the night. Thanks so much for you incredible answer. You're getting lots of upvotes :) – Dylan Gattey Aug 19 '11 at 04:16
  • @MarqueIV "This lets you play items from your iPod library in the background and still get remote events" Do you just register remote events as you would for `AVAudioPlayer`? – sooper Feb 22 '12 at 17:26
  • Been a while, but I believe yes. You just fire up the different player (AVPlayer, not AVAudioPlayer). Make sure to set the category correctly however. That'll bite ya if you don't. – Mark A. Donohoe Feb 22 '12 at 17:40
  • I've managed to get the player working fine, in the background. But I'm not receiving any remote control events. I've tried creating instance of `AVAudioPlayer` (along with `AVPlayer`) and setting it's delegate property to `self`, but still nothing. I've inserted `[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];` in `viewDidLoad` and still no luck. Do you have any suggestions? – sooper Feb 23 '12 at 13:03
  • Never-mind, I hadn't set the view controller as first responder! Thanks – sooper Feb 23 '12 at 18:27
  • I think your issue is that you're creating the AVPlayer. As I said, you can't do that. You can *only* use AVAudioPlayer. Not sure why that is, but I've found if you do create AVPlayer, it steals the remote control events from you. Just follow my steps above *only* and you should be just fine. – Mark A. Donohoe Feb 23 '12 at 23:53
  • @MarqueIV - Hi, your answer says use AVPlayer and you second last comment above, says use AVAudioPlayer. Which one is correct? I am using AVAudioPlayer and I dont seem to be getting any remote control events other than the volume, and my app name is in the iPod controls top bar. – some_id Mar 04 '13 at 13:19
  • Ooops! Good catch! You're right. My last comment seems to have gotten them backwards. You have to use AVPlayer, not AVAudioPlayer just like the answer and my other comments say. – Mark A. Donohoe Jun 26 '13 at 23:13
  • Have you a released app with this method? I found in Review Guidelines that you have to use MPMediaPlayerController to play iTunes library: “9.1 Apps that do not use the MediaPlayer framework to access media in the Music Library will be rejected” https://developer.apple.com/appstore/resources/approval/guidelines.html – Pavel Alexeev Jun 11 '14 at 04:41
  • Also, AVPlayer will not update the bookmarkTime property of audiobooks :( – Hackmodford Jul 30 '15 at 14:56
2

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

Blaise
  • 21
  • 1
0

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
  • 33,113
  • 21
  • 131
  • 183
  • 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. – Mark A. Donohoe Jul 07 '10 at 21:44
  • 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/ – Mark A. Donohoe Jul 09 '10 at 00:22
0

One think I noticed about MPMediaItemPropertyAssetURL is that, although the object returned is in NSURL but the absoluteString is something like this:

ipod-library://item/item.mp3?id=580807975475997900

Which is not what AVAudioPlayer want. What AVAudioPlayer want is NSURL object that is created from a file with a valid file path.

And I have no idea how to get file path from MPMediaItem. So I guess maybe AVPlayer is the way to go if you want to play iPod track without using MPMusicPlayer.

TuanCM
  • 203
  • 1
  • 4
  • 12