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!
Ok! Merci beaucoup! Mais, comment puis-je l'obtenir où il se déconnecte entre les actions audioPlayer.play() et audioPlayer.pause()? – iFunnyVlogger
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
Merci beaucoup! Le seul problème est que l'App Delegate ne connaît pas le lecteur audio. – iFunnyVlogger