2017-04-14 5 views
0

Je fais une application musicale pour tvOS qui a un AVAudioPlayer. Je me demandais comment faire le bouton Lecture/Pause sur Apple TV à distance Lecture/Pause AVAudioPlayer? Voici mon code actuel:Comment faire un bouton Lecture/Pause sur Apple TV Remote Lecture/Pause AVAudioPlayer

import UIKit 
import AVFoundation 


class MusicViewController: UIViewController, AVAudioPlayerDelegate { 

    @IBOutlet weak var progressView: UIProgressView! 


    var audioPlayer = AVAudioPlayer() 

    override func viewDidLoad() { 
     super.viewDidLoad() 




     do { 

      audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "Roots", ofType: "mp3")!)) 

      audioPlayer.prepareToPlay() 

      var audioSession = AVAudioSession.sharedInstance() 

      Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(updateAudioProgressView), userInfo: nil, repeats: true) 
      progressView.setProgress(Float(audioPlayer.currentTime/audioPlayer.duration), animated: false) 


      do { 

       try audioSession.setCategory(AVAudioSessionCategoryPlayback) 

      } 

     } 

     catch { 

      print(error) 

     } 

     audioPlayer.delegate = self 

    } 


    // Set the music to automaticly play and stop 

    override func viewDidAppear(_ animated: Bool) { 
     audioPlayer.play() 


    } 

    override func viewDidDisappear(_ animated: Bool) { 
     audioPlayer.stop() 

    } 

    func updateAudioProgressView() 
    { 
     if audioPlayer.isPlaying 
     { 
    // Update progress 
      progressView.setProgress(Float(audioPlayer.currentTime/audioPlayer.duration), animated: true) 
     } 
    } 


} 

J'ai regardé autour d'essayer de comprendre cela. Je n'ai jamais travaillé avec tvOS auparavant, donc c'est nouveau pour moi. Merci beaucoup pour l'aide!

Répondre

0

Ces fonctions ont travaillé pour nous. Ils ajoutent un détecteur de mouvement qui écoute le bouton lecture/pause de la télécommande. Vous pouvez ajouter ceci au délégué de votre application.

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() 
    } 
} 
+0

Ok! Merci beaucoup! Mais, comment puis-je l'obtenir où il se déconnecte entre les actions audioPlayer.play() et audioPlayer.pause()? – iFunnyVlogger

+0

Voir la mise à jour. Vous pouvez avoir besoin de logique supplémentaire ici, en fonction de ce que vous faites, mais cela devrait vous aider à démarrer. – picciano

+0

Merci beaucoup! Le seul problème est que l'App Delegate ne connaît pas le lecteur audio. – iFunnyVlogger