22

A good example of what I'm trying to accomplish is implemented in the latest version of the Spotify iPhone application for (Pandora seems to have the same feature) .

When Spotify is in the background, double tapping opens the "multi-task dock", where the ipod controls (play/pause, forward etc) allow to control the music playback of Spotify (not the ipod application). Also, when the iphone/ipod touch is locked, double tapping displays similar playback controls.

If you don't know what I mean, here's an article that has screenshots : http://www.wired.com/gadgetlab/2010/07/spotify-updated-for-ios4-ready-to-replace-ipod/

In my current application, music is streamed from a server (using Matt Gallagher's AudioStreamer). I've managed to keep the music playing in the background. Now, I'd like to link my playback to the "multi-task dock"/lock screen.

Should I be using [MPMusicPlayerController iPodMusicPlayer] ? How should I proceed ?

Bonus question : if you can tell me how to change the ipod icon to my application icon in the "multi-task dock" (Spotify pulled that trick as well...), that whould be AWESOME.

Any help appreciated, thanks.

zoul
  • 102,279
  • 44
  • 260
  • 354
Max
  • 1,054
  • 1
  • 12
  • 20

3 Answers3

31

Problem is solved.

In short, to enable remote control event, 1) use :

- (void)remoteControlReceivedWithEvent:(UIEvent *)theEvent

and 2) put this is your view controller :

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}
- (BOOL)canBecomeFirstResponder {
    return YES;
}

I have to give credit to Grant. He has forked Matt Gallagher's AudioStreamer enabling all the ios4 improvements (background audio, and remote controls working). You can find his sources along with a working sample on github : http://github.com/DigitalDJ/AudioStreamer

Regarding the icon : once you use beginReceivingRemoteControlEvents, the icon automatically switches to your app icon. Brilliant !

Max
  • 1,054
  • 1
  • 12
  • 20
  • 2
    Thanks! But, you forgot something. Even though I adjusted my code (adding remoteControlReceivedWithEvent and the stuff inside viewDidAppear), nothing happend when pressing previous/play/pause/next buttons in the ipod controls. The missing piece was, I needed this method: -(BOOL)canBecomeFirstResponder { return YES; } – Jonny Aug 19 '10 at 07:33
  • I'll have to comment again. I thought all of the above would fix it, but even after running this successfully a few times, there are times when the events just don't arrive at the view controller where I did the becomeFirstResponder and all. It's like it just stops working at some point, and I've yet to figure out what could be causing the problem. – Jonny Aug 19 '10 at 09:13
  • did you find any solution to that? in my subclass of MPMoviePlayerViewController the remoteControlReceivedWithEvent method isn't being called... – Faizan S. Jul 31 '11 at 23:53
  • does somebody know how to pass the name of the music to be displayed on the lock screen as the ipod does? – Roberto Ferraz Sep 20 '11 at 04:25
  • Do you know of any other app that does this ? I'm not sure it's possible. I'm using Spotify right now and it doesn't look like it will display the title... But if you know it's possible, I'm definitely interested in how to do this. – Max Sep 20 '11 at 14:23
  • did you use applicationmusicplayer or ipodmusicplayer? – Eric Apr 19 '12 at 23:48
  • @Eric No. I only used Matt Gallagher's AudioStreamer as I needed to stream a radio. – Max Apr 20 '12 at 09:27
  • You are missing one `*` in `- (void)remoteControlReceivedWithEvent:(UIEvent)theEvent`. Here: `(UIEvent *)`. – mAu May 22 '12 at 09:55
  • 1
    @RobertoFerraz To show the current title and album artwork for the music your app is playing you can use `MPNowPlayingInfoCenter` - it's new with iOS 5. – Udi May 29 '12 at 16:00
2

Here's the documentation:

http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/RemoteControl/RemoteControl.html

Notice however, that it'll work only when you have active audio session in your application.

I'm using it with AVAudioSession with AVAudioSessionCategoryPlayback category and AVAudioPlayer and "remote controls" work only when I have AVAudioSession active and AVAudioPlayer object created.

grzaks
  • 1,404
  • 2
  • 18
  • 31
1

The controls will change for your application if you are using the new background audio api's. Information can be found here. Specifically the sections about background audio.

Elfred
  • 3,851
  • 22
  • 16
  • I've read that page multiple times already. It seems (just like for background audio), that once UIBackgroundModes is set to audio, the controls should work by themselves. In my experience, this is not the case. I'm guessing the problem is that I'm streaming music, not playing it from the device, or perhaps I've missed something. Perhaps my Audiostreamer isn't set right. To be more specific : to anyone who has ever managed to do this, did you use [musicPlayer beginGeneratingPlaybackNotifications] and hook the following notifications to your own streaming player ? – Max Jul 08 '10 at 08:41
  • You should be using AVAudioSessions. – Elfred Jul 08 '10 at 13:22