AVAudioPlayer音频在设置和播放时不会播放

时间:2016-07-30 21:21:17

标签: ios swift audio swift2 avaudioplayer

我有一个具有不同按键按下声音的应用程序。我正在尝试使用两个功能来设置下一个按钮按下声音,然后播放它。我们的想法是在按下下一个按钮时加载音频文件,然后在按下按钮时最后通过不同的功能播放它。使用prepareToPlay(),这可以让播放更快。

问题是,当我按下按钮时,我听到'沉默'。我说'沉默'是因为我的发言者实际上收到了一些信号,但是它也是一周的时间。

这是我的代码:

func loadNextButtonPressSound() {

    let randomSoundIndex = Int(arc4random_uniform(UInt32(pressSounds.count)))
    pressSoundPlayer = try! AVAudioPlayer(contentsOfURL: pressSounds[randomSoundIndex])
    pressSoundPlayer.prepareToPlay()
    pressSoundPlayer.delegate = self

}


func playButtonPressSound() { // called when a button is pressed

    if(StoredSettings.instance.getEnableSoundsSetting()) {
        pressSoundPlayer.play()
        loadNextButtonPressSound()
    }
}

如果我改为将代码合并形成:

内部功能playButterPressSound(){

if(StoredSettings.instance.getEnableSoundsSetting()) {
    let randomSoundIndex = Int(arc4random_uniform(UInt32(pressSounds.count)))
    pressSoundPlayer = try! AVAudioPlayer(contentsOfURL: pressSounds[randomSoundIndex])
    pressSoundPlayer.play()

}

}

它开始工作(即我听到按钮按下),但它失败了使用prepareToPlay()预加载下一个声音的目的。

有关如何使上面的第一个代码生效的任何建议吗?

0 个答案:

没有答案
相关问题