rapide. AVPlayer. Comment suivre quand la chanson a fini de jouer?

Quel est le path de l'est à suivre quand la chanson a fini de jouer avec AVPlayer dans Swift?

Y at-il une fonction qui est appelée quand avplayer fini de jouer, ou je devrais combiner la timer avec des references de class avplayer?

Quelque chose comme ça fonctionne:

func play(url: NSURL) { let item = AVPlayerItem(URL: url) NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: item) let player = AVPlayer(playerItem: item) player.play() } func playerDidFinishPlaying(note: NSNotification) { // Your code here } 

N'oubliez pas d'enlever l'observateur lorsque vous avez terminé (ou deinit )!

Vous devez créer un object qui implémente le protocole AVAudioPlayerDelegate et l'utiliser en tant que délégué de l'object AVAudioPlayer . Puis reliez-les set, par exemple:

 audioPlayer = try! AVAudioPlayer(contentsOf: audioFileUrl) audioPlayer.delegate = self 

Le délégué peut implémenter des methods qui répondent à certains events. Celui-ci se triggers quand l'audio finit de jouer:

 func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) { // ... } 

Une autre version pour Swift 3

 NotificationCenter.default.addObserver(self, selector: #selector(self.playerDidFinishPlaying(sender:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: item) func playerDidFinishPlaying(sender: Notification) { // Do Something } 

Pour Swif3, vous devrez changer comme suit:

 func play(url: NSURL) { let item = AVPlayerItem(URL: url) NotificationCenter.default.addObserver(self,selector:Selector("playerDidFinishPlaying"), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: item) let player = AVPlayer(playerItem: item) player.play() } func playerDidFinishPlaying() { // Your code here } 

une solution plus complète est ici:

 import UIKit import AVFoundation import MediaPlayer class ViewController: UIViewController,AVAudioPlayerDelegate { var player: AVAudioPlayer = AVAudioPlayer() @IBAction func play(_ sender: UIButton) { player.play() player.currentTime=14*60-10 print(player.currentTime) } @IBAction func pause(_ sender: UIButton) { player.pause() } @IBAction func replay(_ sender: UIButton) { player.currentTime=0 } override func viewDidLoad() { super.viewDidLoad() do{ let audioPath = Bundle.main.path(forResource: "elon", ofType: "mp3") player = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: audioPath!)) player.prepareToPlay() player.delegate = self } catch{ print(error) } } func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool){ print(flag) print("here") if flag == true{ } } } 
 import AVFoundation var AVPlayerCustom:AVAudioPlayer = AVAudioPlayer() class PlayerModule: NSObject, AVAudioPlayerDelegate { func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) { print("Finish") } func playWithData(data: Data, proc: Int) { //print(data) do { AVPlayerCustom = try AVAudioPlayer(data: data) AVPlayerCustom.delegate = self as! AVAudioPlayerDelegate AVPlayerCustom.prepareToPlay() AVPlayerCustom.play() } catch { print("error1") } } }