swift. AVPlayer. How to track when song finished playing?

前端 未结 7 585
春和景丽
春和景丽 2021-01-30 13:51

What is the east way to track when song finished playing with AVPlayer in Swift?

Is there any function which is called when avplayer finished playing, or I should combin

相关标签:
7条回答
  • 2021-01-30 14:34

    a more complete solution is here:

    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{
    
            }
        }
    
    
    }
    
    0 讨论(0)
  • 2021-01-30 14:35
    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")
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-30 14:36

    Something like this works:

    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
    }
    

    Don't forget to remove the observer when you're done (or in deinit)!

    0 讨论(0)
  • 2021-01-30 14:46

    for Swift 4.2

    func play(url: URL) {
        let item = AVPlayerItem(url: url)
        NotificationCenter.default.addObserver(self, selector: #selector(self.playerDidFinishPlaying(sender:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: item)
    
        let player = AVPlayer(playerItem: item)
        player.play() 
    }
    
    @objc func playerDidFinishPlaying(sender: Notification) {
        // Your code here
    }
    
    0 讨论(0)
  • 2021-01-30 14:47

    Another version for Swift 3

    NotificationCenter.default.addObserver(self, selector: #selector(self.playerDidFinishPlaying(sender:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: item)
    
    func playerDidFinishPlaying(sender: Notification) {
    
        // Do Something
    }
    
    0 讨论(0)
  • 2021-01-30 14:51

    You need to create an object that implements the AVAudioPlayerDelegate protocol, and use that as the delegate of the AVAudioPlayer object. Then link them together, for example:

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

    The delegate can implement methods that responds to certain events. This one fires when the audio finishes playing:

    func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
        // ...
    }
    
    0 讨论(0)
提交回复
热议问题