2017-04-15 86 views
0

我的應用程序中有一個AVAudioPlayer,我添加了一個功能,用戶可以按遙控器上的播放/暫停按鈕來播放/暫停AVAudioPlayer。我在我的App Delegate中有這樣的代碼:將操作添加到Apple TV遙控器播放/暫停按鈕

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的名稱是audioPlayer。我在代碼中說過,但App Delegate沒有從PlayerViewController引用AVAudioPlayer。我該怎麼做呢?

回答

0

您的代碼看起來正確。只是缺少細節,可以解釋爲什麼它不能按預期工作......在tvOS,除了設置allowedPressTypes它也需要定義allowedTouchTypes間接:

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

謝謝,但我已經補充說。問題是,應用程序委託不知道什麼audioPlayer是「使用未解析的標識符'audioPlayer' – iFunnyVlogger