我正在尝试允许 Remote 或耳塞在应用处于后台或屏幕锁定时播放和暂停音频。所以在 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 中设置。我很茫然。
最佳答案
您可能还需要添加:
- (BOOL)canBecomeFirstResponder {
return YES;
}
UIResponder
的任何子类都需要实现这个,否则它不会接受第一响应者状态。
关于iphone - remoteControlReceivedWithEvent 从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13427572/