我正在为具有AVAudioPlayer的tvOS创建应用。我希望Apple TV Remote的播放/暂停按钮播放/暂停AVAudioPlayer。这是我到目前为止在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)
playButtonRecognizer.allowedTouchTypes = [NSNumber(value: UITouchType.indirect.rawValue)]
}
func handlePlayButton(_ sender: AnyObject) {
if audioPlayer.isPlaying {
audioPlayer.pause() {
} else {
audioPlayer.play()
}
}
}
问题是App Delegate对AVAudioPlayer(audioPlayer)一无所知。 audioPlayer是离乐MusicViewController。如何让App Delegate知道audioPlayer是什么?我收到此错误 - 使用未解析的标识符' audioPlayer'
答案 0 :(得分:0)
您是否真的创建了AVAudioPlayer的实例?
let player = try AVAudioPlayer(contentsOf: url)
player.play()