Reputation: 171
i am making app with Xcode using Swift and i have mp3 file in my app and i have added Play and Pause Button in it but my problem is that when i click on Play button Pause button shows but when mp3 file finished playing there is still pause button display and i want it to change to Play button again automatically when audio finished . this is my code
@IBAction func buttonPressed(_ sender: UIButton) {
if player.isPlaying {
player.stop()
sender.setImage(UIImage(named:"play.png"),for: .normal)
} else {
player.play()
sender.setImage(UIImage(named:"pause.png"),for: .normal)
}
}
Thanks
Upvotes: 0
Views: 346
Reputation: 322
You need to implement AVAudioPlayerDelegate in class:
class ViewController: UIViewController, AVAudioPlayerDelegate {
...
and use this function inside class:
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
button.setImage(UIImage(named:"pause.png"),for: .normal)
}
Upvotes: 2