Web Audio APi无法执行'createMediaElementSource'

问题描述 投票:1回答:1

[我正在尝试使用网络音频Api同时播放多首曲目(最终目标是基于地理位置创建地理空间声音引擎]

我无法将两个或多个html5音频源正确连接到音频上下文。但是在非常简单的用例中,它可以与命令式代码一起使用。

这里有两个例子:

第一个不可扩展但可在Chrome中工作的]:>

window.addEventListener('load', async function(e) {

    let file = await stansardAudioLoader('./assets/fire.mp3')
    let file1 = await stansardAudioLoader('./assets/pluie.wav')

    let source = audioContext.createMediaElementSource(file)
    let source1 = audioContext.createMediaElementSource(file1)

    source.connect(audioContext.destination)
    source1.connect(audioContext.destination)

    file.play()
    file1.play()

 }, false);

第二个更易于管理,确实接近第一个,但不起作用。

   const loadAndPlay = (ctx) => async (url) =>{

    const file = await stansardAudioLoader(url)
    const source = ctx.createMediaElementSource(file)

    source.connect(ctx.destination)
    file.play()
}

window.addEventListener('load', async () => {

    const audioContext = new (AudioContext || webkitAudioContext)();
    const loader =  loadAndPlay(audioContext)

    loader('./assets/fire.mp3')
    loader('./assets/pluie.wav')

 }, false);

这是两个过于简化的示例,以使其易于测试和复制。我的完整代码使用OOP和基于类的方法。可能会加载,播放和更新数百个文件。

两件事对我有帮助:

  • 如何使用Web音频api而不同时使用context.decodeAudioData来同时播放两个或多个声音
  • 有关如何使用网络音频API处理大量文件和空间音频的一般指南
  • 谢谢您的帮助!

我正在尝试使用网络音频Api同时播放多首曲目(最终目标是基于地理位置创建地理空间声音引擎),我无法设法正确连接两个或多个……

javascript audio web-audio-api
1个回答
2
投票

如果可以从音频中提取/解码Float32样本,则可以一起避免decodeAudioDatafetch-stream-audio示例显示了如何立即大块地使用Opus和WAV文件来完成。 (我也建议通过WAV和MP3使用Opus。)

对于空间音频,您可以安排AudioBufferSourceNode.start()同时播放多个音频

© www.soinside.com 2019 - 2024. All rights reserved.