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

前端 未结 7 596
春和景丽
春和景丽 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{
    
            }
        }
    
    
    }
    

提交回复
热议问题