mjk
mjk

Reputation: 21

audioPlayerDidFinishPlaying usage

 class ViewController: UIViewController, AVAudioPlayerDelegate {    
 func audioPlayerDidFinishPlaying (_ player:AVAudioPlayer, successfully flag: Bool) {
    endcheck.text = "end"

}

@IBAction func play(_ sender: Any) {
    do
    {
        let url = Bundle.main.path (forResource: String(myIndex), ofType: "mp3")
        try player = AVAudioPlayer (contentsOf: NSURL(fileURLWithPath: url!) as URL)
    }
    catch{
    }

    player.play()


    let hasThePlayerEnded = audioPlayerDidFinishPlaying(_:player, successfully:true)

}
    super.viewDidLoad()
   self.player.delegate = self

}

Right at the moment when I click "play" button, I see "end" not when a sound has finished playing. I'm sure I did something wrong. how should I fix it?

Upvotes: 0

Views: 292

Answers (1)

MBT
MBT

Reputation: 1391

Remove the following statement from play() method

let hasThePlayerEnded = audioPlayerDidFinishPlaying(_:player, successfully:true) 

Upvotes: 1

Related Questions