我正在尝试播放一段可以暂停、继续或停止的声音片段。我的暂停按钮起作用了,这个按钮的 Action 函数如下所示。但是我无法使我的简历功能正常工作。当我按下恢复按钮时,没有播放音频。我在网上阅读过,我能找到的唯一提示是使用 prepareToPlay() 函数并将 shortStartTimeDelay 设置为大于 0.0 的值。我已经尝试了这两个都无济于事。
timeAtPause 是一个 NSTimeInterval 类型的全局变量
暂停按钮的 Action 函数如下:
@IBAction func pauseAllAudio(sender: UIButton) {
timeAtPause = audioPlayer.currentTime
audioPlayer.pause()
}
恢复按钮的 Action 函数如下:
@IBAction func resumeAllAudio(sender: UIButton) {
let shortStartDelay = 0.01
audioPlayer.prepareToPlay()
audioPlayer.playAtTime(timeAtPause + shortStartDelay)
}
如能提供有关如何恢复音频的任何提示,我们将不胜感激。 感谢您的宝贵时间。
最佳答案
您不应该使用playAtTime()
来恢复AVAudioPlayer
,因为documentation状态:
Plays a sound asynchronously, starting at a specified point in the audio output device’s timeline.
和
Use this method to precisely synchronize the playback of two or more AVAudioPlayer objects.
而且,即使你使用它,它也应该与 deviceCurrentTime
加上以秒为单位的时间一起使用,以产生延迟。一言以蔽之,它并不意味着用于恢复暂停的播放器。相反,只需使用 play()
来恢复播放。
关于ios - 快速恢复 AVAudioPlayer 中的声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35379401/