Posted by: bearc0025 | February 6, 2012

How to Build an App: iOS Background Audio Controls

Allowing your app to receive remote audio control events is pretty easy.

The gist of it is to just tell the OS you’re interested in being told of the events. Then you just have a callback for the events and you can act on them.

For the view controller that’s playing music, you can declare interest in receiving the events in the viewWillAppear: method like this…

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

Do the reverse in the viewWillDisappear: like this…

- (void)viewWillDisappear:(BOOL)animated {
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    [self resignFirstResponder];
    [super viewWillDisappear:animated];
}

Then you’re ready to handle the events…

- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
    if (receivedEvent.type == UIEventTypeRemoteControl) {
        switch (receivedEvent.subtype) {
            case UIEventSubtypeRemoteControlTogglePlayPause:
                [self togglePlayPause];
                break;
            case UIEventSubtypeRemoteControlPreviousTrack:
                [self playPrevTrack];
                break;
            case UIEventSubtypeRemoteControlNextTrack:
                [self playNextTrack];
                break;
            default:
                break;
        }
    }
}

See details here in Apple documentation.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: