ios - 快速恢复 AVAudioPlayer 中的声音

标签 ios swift avaudioplayer resume

我正在尝试播放一段可以暂停、继续或停止的声音片段。我的暂停按钮起作用了,这个按钮的 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/

相关文章:

ios - 在 TabBarController 项中有一个 NavigationController

objective-c - AVAudioPlayer Number Of Loops 只播放一遍后生效

iphone - AVAudioPlayer 在播放视频时无法识别设备静音按钮

ios - iPhone:检测自上次屏幕触摸以来的用户不活动/空闲时间

ios - fatal error : Array index out of range when accessing an array of 10 elements in Swift

ios - 遍历数组(Swift)

iphone - AVAudioPlayer 和随机慢度

ios - 从 "SecTrustCopyPublicKey"调用收到的值的含义

iphone - UITableViewController 中的静态单元格在同一 Nib 上打开不同的 URL

string - 从字符串中删除最后一个字符。 swift 语言