向Apple TV的“远程播放/暂停"按钮添加操作 [英] Add Action To Apple TV Remote Play/Pause Button

查看:185
本文介绍了向Apple TV的“远程播放/暂停"按钮添加操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个AVAudioPlayer,我要添加一个功能,用户可以按遥控器上的播放/暂停"按钮来播放/暂停AVAudioPlayer.我的应用程序委托中包含以下代码:

I have an AVAudioPlayer in my app and I am adding a function where the user can press the Play/Pause button on the remote to Play/Pause the AVAudioPlayer. I have this code in my App Delegate:

func initializePlayButtonRecognition() {
    addPlayButtonRecognizer(#selector(AppDelegate.handlePlayButton(_:)))
}

func addPlayButtonRecognizer(_ selector: Selector) {
    let playButtonRecognizer = UITapGestureRecognizer(target: self, action:selector)
    playButtonRecognizer.allowedPressTypes = [NSNumber(value: UIPressType.playPause.rawValue as Int)]
    self.window?.addGestureRecognizer(playButtonRecognizer)
}

func handlePlayButton(_ sender: AnyObject) {
    if audioPlayer.isPlaying {
        audioPlayer.pause() {
        } else {
            audioPlayer.play()
        }
    }
}

AVAudioPlayer的名称是音频播放器.我已经在代码中声明了这一点,但是App Delegate没有从PlayerViewController引用AVAudioPlayer.我该怎么做呢?

The AVAudioPlayer's name is an audio player. I have that stated in the code, but the App Delegate does not have a reference to the AVAudioPlayer from the PlayerViewController. How do I do this?

推荐答案

您的代码看起来正确.只是缺少的一个细节可以解释为什么它无法在tvOS中正常工作...除了设置allowedPressTypes之外,还需要将allowedTouchTypes定义为间接的:

Your code looks correct. Just a missing detail that could explain why it does not work as expected... in tvOS, apart from setting allowedPressTypes it is also needed to define allowedTouchTypes as indirect:

playButtonRecognizer.allowedTouchTypes = [NSNumber(value: UITouchType.indirect.rawValue)]

这篇关于向Apple TV的“远程播放/暂停"按钮添加操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆