我的应用程序中有一个AVAudioPlayer,我正在添加一个功能,用户可以按遥控器上的播放/暂停按钮来播放/暂停AVAudioPlayer。我的应用程序委托中有以下代码:
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的名称是一个音频播放器。我在代码中说明了这一点,但应用程序代表没有从PlayerViewController引用AVAudioPlayer。我该怎么做?
你的代码看起来是正确的。只是一个缺失的细节,可以解释为什么它不能按预期工作......在 tvOS 中,除了设置allowedPressTypes
还需要将allowedTouchTypes
定义为间接:
playButtonRecognizer.allowedTouchTypes = [NSNumber(value: UITouchType.indirect.rawValue)]