我正在尝试允许远程或耳塞播放并在应用处于后台或屏幕锁定时暂停音频。所以在viewDidLoad中我有:
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
然后我有:
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
[self playOrStop];
break;
case UIEventSubtypeRemoteControlPreviousTrack:
break;
case UIEventSubtypeRemoteControlNextTrack:
break;
default:
break;
}
}
}
但是,它永远不会被召唤。在后台运行已打开,AVSession在AppDelegate中设置。我很茫然。
答案 0 :(得分:2)
您可能还需要添加:
- (BOOL)canBecomeFirstResponder {
return YES;
}
UIResponder
的任何子类都需要实现此功能,否则它将不接受第一响应者状态。
答案 1 :(得分:1)
尝试放
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
viewWillAppear中的:( BOOL)动画而不是viewDidLoad。