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

前端 未结 7 583
春和景丽
春和景丽 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: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")
            }
        }
    }
    

提交回复
热议问题