2017-04-17 48 views
0

我正在爲具有AVAudioPlayer的tvOS創建應用程序。我想讓Apple TV Remote的播放/暫停按鈕播放/暫停AVAudioPlayer。這裏是我的代碼,我在App代表補充至今:參考應用程序委託中的AVAudioPlayer(tvOS)

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委託不知道有關AVAudioPlayer(audioPlayer)任何東西。 audioPlayer聲明爲MusicViewController。如何讓App Delegate瞭解audioPlayer是什麼?我得到這個錯誤 - 使用未解析的標識符'audioPlayer'

+0

重複:HTTP://stackoverflow.com/questions/43428807/add-action-to-apple-tv-remote-play-pause-button的[添加操作Apple TV的遙控遊玩 –

+0

可能的複製/暫停按鈕](http://stackoverflow.com/questions/43428807/add-action-to-apple-tv-remote-play-pause-button) –

回答

0

那麼你真的創建了一個AVAudioPlayer的實例嗎?

let player = try AVAudioPlayer(contentsOf: url) 
player.play()