在视频播放期间响应apple tv控件

Sma*_*kon 5 controls ios apple-tv tvos

我有一个带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];
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激,谢谢!

小智 -1

您使用 UITapGestureRecognizer 来检测 tvOS 上的按钮点击。设置 allowedPressType 属性(例如@[@(UIPressTypePlayPause)])。

\n\n

但是,您不应该在 tvOS 上实现自定义交互式播放 UI。相反,请使用 AVPlayerViewController (AVKit)。您的自定义播放器将大大低于用户的期望(Siri 命令不起作用,某些遥控器不起作用,您的手势将略有不同,等等)。

\n\n

如果您发现 AVPlayerViewController 在其他平台上限制太多,请再看一下\xe2\x80\x94tvOS 上有许多用于增强播放的新 API,包括外部元数据和插页式广告。

\n