我有一个带有 AVPlayer 的应用程序来播放视频。我希望能够检测到用户何时按下了遥控器上的按钮。我尝试实现以下方法,但是 remoteControlReceivedWithEvent() 不响应任何控制事件。
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
//if it is a remote control event handle it correctly
if (event.type == UIEventTypeRemoteControl) {
if (event.subtype == UIEventSubtypeRemoteControlPlay) {
NSLog(@"play");
} else if (event.subtype == UIEventSubtypeRemoteControlPause) {
NSLog(@"pause");
} else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause) {
NSLog(@"toggle");
}
}
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)viewDidAppear:(BOOL)animated {
// Setup Apple TV control events
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
// Remove Apple TV control events
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
}
任何帮助将不胜感激,谢谢!