问题
I've got couple of short .mp3 sounds which I'm storing in the array and would like to play them consecutively. Is there any way to detect when AVAudioPlayer stopped playing so that I could call a completion handler and play next sound? I know that there is a delegate for that but I'm using Playground and SKScene.
回答1:
Hellp @codddeer123,
AVAudioPlayer contains AVAudioPlayerDelegte method which is called on audio playing completed.
I have implemented it as extension of my class, you can do as you likes.
extension MyViewController: AVAudioPlayerDelegate {
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
if flag {
// After successfully finish song playing will stop audio player and remove from memory
print("Audio player finished playing")
self.audioPlayer?.stop()
self.audioPlayer = nil
// Write code to play next audio.
}
}
}
You can write your code here to play next audio. As per your question I have posted this answer. If you don't know about AVAudioPlayer then go for apple official documentation or other online tutorials.
I hope this will help you.
来源:https://stackoverflow.com/questions/49339216/detecting-end-of-the-playback-in-avaudioplayer