问题描述
这里有一个使用 AVAudioPlayer 的例子。在描述中它说它能够:
- 通过可选的同步同时播放多个声音。
我在示例中看不到如何做到这一点。
表示相同内容的 Apple API:
- 通过同步多个播放器的播放来同时播放多个声音
https://developer.apple.com/documentation/avfaudio/avaudioplayer?language=objc
示例:
https://github.com/xamarin/docs-archive/tree/master/Recipes/ios/media/sound/avaudioplayer
注意:存储库已存档,不允许添加问题。
解决方法
对您想要的所有声音使用 playAtTime() 方法,并将同一日期传递给所有要同时播放的声音。
我阅读了 playAtTime() 方法并认为它是“在声音的时间在这个位置播放”,因为它说它说参数被命名为时间而不是日期:
但它实际上需要一个日期,这意味着在未来的日期和时间播放。
因此,如果您只查看自动完成 API 并且显示 playAtTime(time)
,那么您在查看文档时不会获得详细信息。看到声音播放器还有另一个属性 currentTime 是一个数字而不是日期。
文档:
异步播放音频,从音频中的指定点开始 输出设备的时间线。
func startSynchronizedPlayback() {
// Create a time offset relative to the current device time.
let timeOffset = playerOne.deviceCurrentTime + 0.01
// Start playback of both players at the same time.
playerOne.play(atTime: timeOffset)
playerTwo.play(atTime: timeOffset)
}