Detecting end of the playback in AVAudioPlayer

天大地大妈咪最大 提交于 2019-12-11 05:32:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!