3

I'm trying to allow a remote or earbuds to play and pause audio when the app is in background or the screen is locked. So In viewDidLoad I have:

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

I then have:

- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {



if (receivedEvent.type == UIEventTypeRemoteControl) {



switch (receivedEvent.subtype) {



    case UIEventSubtypeRemoteControlTogglePlayPause:

        [self playOrStop];

        break;



    case UIEventSubtypeRemoteControlPreviousTrack:


        break;



    case UIEventSubtypeRemoteControlNextTrack:


        break;



    default:

        break;

}

}


}

But, it never gets called. Running in background is turned on, the AVSession is set in AppDelegate. I'm at a loss.

4

2 回答 2

2

You probably also need to add:

- (BOOL)canBecomeFirstResponder {
    return YES;
}

Any subclass of UIResponder needs to implement this, or it won't accept first responder status.

于 2013-04-10T15:54:40.873 回答
1

Try putting

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

in viewWillAppear:(BOOL)animated instead of viewDidLoad.

于 2012-11-17T04:10:42.097 回答