在我的 appdelegate 我有..
NSError *myErr;
NSError *activationError;
AVAudioSession *session = [AVAudioSession sharedInstance];
if( [session setCategory:AVAudioSessionCategoryPlayback error:&myErr] ) {
if( [UIApplication instancesRespondToSelector: @selector (beginReceivingRemoteControlEvents)] ) {
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
[session setDelegate: self];
if( ![session setActive: YES error: &activationError] ) {
NSLog(@"Audio Session error %@, %@- unable to setActive", activationError, [activationError userInfo]);
return false;
}
}
else {
NSLog(@"Audio Session error %@, %@ - unable to setCategory", myErr, [myErr userInfo]);
return false;
}
然后在我的 ViewController 我有..
- (BOOL) canBecomeFirstResponder {
return YES;
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlPlay:
break;
case UIEventSubtypeRemoteControlPause:
break;
case UIEventSubtypeRemoteControlStop:
break;
case UIEventSubtypeRemoteControlTogglePlayPause:
break;
case UIEventSubtypeRemoteControlNextTrack:
break;
case UIEventSubtypeRemoteControlPreviousTrack:
break;
case UIEventSubtypeRemoteControlBeginSeekingBackward:
break;
case UIEventSubtypeRemoteControlEndSeekingBackward:
break;
case UIEventSubtypeRemoteControlBeginSeekingForward:
break;
case UIEventSubtypeRemoteControlEndSeekingForward:
break;
default:
break;
}
}
}
希望这可以帮助。