在AVAudioPlayer中检测播放结束 [英] Detecting end of the playback in AVAudioPlayer

查看:243
本文介绍了在AVAudioPlayer中检测播放结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些短的.mp3声音,我将它们存储在数组中,并希望连续播放它们.有什么方法可以检测AVAudioPlayer何时停止播放,以便我可以调用完成处理程序并播放下一个声音?我知道有一个代表,但是我正在使用Playground和SKScene.

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.

推荐答案

帮助@ codddeer123,

Hellp @codddeer123,

AVAudioPlayer包含AVAudioPlayerDelegte方法,该方法在音频播放完成时被调用.

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.
        }
    }
}

您可以在此处编写代码以播放下一个音频.根据您的问题,我已经发布了此答案.如果您不了解AVAudioPlayer,请查阅Apple官方文档或其他在线教程.

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.

这篇关于在AVAudioPlayer中检测播放结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆