7

I've figured out how I can get audio playing in the background on ios4, however I've noticed that some apps also replace the iPod dock icon with their own app icon. (Last.fm & Spotify for example).

They are also able to use the dock media controls to pause and resume their streams.

Does anyone know how to do this?

Thanks

stitz
  • 1,429
  • 1
  • 16
  • 33
AggroPanda
  • 235
  • 4
  • 10

1 Answers1

18

It's easy you have to respond to the Remote Control Events. This also lets you control your app with the headset.

In lets say viewDidLoad call:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];

And you have to respond to both

- (BOOL)canBecomeFirstResponder {
return YES;
}

And

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    switch (event.subtype) {
        case UIEventSubtypeRemoteControlTogglePlayPause:
            if (audio.rate == 0.0) {
                [audio play];
            } else {
                [audio pause];
            }
            break;
        case UIEventSubtypeRemoteControlPlay:
            [audio play];
            break;
        case UIEventSubtypeRemoteControlPause:
            [audio pause];
            break;
        default:
            break;
    }
}
Bjarne Mogstad
  • 1,154
  • 13
  • 18
  • awesome thanks, I'll give it a try. do you have any ideas about replacing the iPod dock icon with my own app icon? – AggroPanda Jul 30 '10 at 09:57
  • By implementing this Apple will putt your icon in the dock – Bjarne Mogstad Aug 01 '10 at 19:55
  • 2
    In fact you must call becomeFirstResponder inside of viewDidAppear otherwise it doesn't work and you never receive remote control events. http://stackoverflow.com/questions/3456435/ios-4-remote-controls-for-background-audio – this post is more useful. – pronebird May 19 '11 at 10:56