WebAudio API 在 Google Chrome 上的不同行为

Different behaviour of WebAudio API on Google Chrome

我正在用频谱可视化工具制作声音播放器。在 Firefox 上运行良好,但在 Google Chrome 中我遇到了问题。我来自前几天我做的这个问题

在 Firefox 上,我可以随时在曲目列表中前进或前进,没有问题,但在 Google Chrome 中,当我按 [=35= 时出现此错误]

Failed to execute 'createMediaElementSource' on 'BaseAudioContext': 
HTMLMediaElement already connected previously to a different MediaElementSourceNode.

我不知道为什么 Google Chrome 抱怨而 Firefox 没有。可视化工具的代码是:

 function visualizer(audio) {
    closeAudioContext = true;
    let src = context.createMediaElementSource(audio);  // Here fails on Google Chrome
    let analyser = context.createAnalyser();
    let canvas = document.getElementById("canvas");
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
    let ctx = canvas.getContext("2d");

    src.connect(analyser);
    analyser.connect(context.destination);

    analyser.fftSize = 2048;

    let bufferLength = analyser.frequencyBinCount;

    let dataArray = new Uint8Array(bufferLength);

    let WIDTH = ctx.canvas.width;
    let HEIGHT = ctx.canvas.height;

    let barWidth = (WIDTH / bufferLength) * 1.5;
    let barHeight;
    let x = 0;

    let color = randomColor();

    function renderFrame() {
      requestAnimationFrame(renderFrame);
      x = 0;
      analyser.getByteFrequencyData(dataArray);
      ctx.clearRect(0, 0, WIDTH, HEIGHT);

      for (let i = 0; i < bufferLength; i++) {
        barHeight = dataArray[i];

        ctx.fillStyle = color;
        ctx.fillRect(x, HEIGHT - barHeight, barWidth, barHeight);
        x += barWidth + 1;

      }
    }

    renderFrame();
  }

按下 "next/previous" 按钮后我立即执行:

if (closeAudioContext && context !== undefined) {
      context.close();
}
context = new AudioContext();

然后:

visualizer(audio);
musicPlay();

所以我的问题是,为什么音频播放器在 Firefox 中工作正常但在 Google Chrome 中崩溃?

我正在使用 Bowser 检查用户使用的是什么浏览器,因为作为 Chrome 的新政策,如果它激活了自动播放(在这种情况下我设置了自动播放为 false,如果用户按下播放声音,则不会静音)。所以,如果我必须为 Google Chrome 创建一个不同的代码,我可以用那个代码创建一个 "if"。

此致。

这是 Chrome 中的错误;您不能将 HTMLMediaElement 附加到另一个 MediaElementAudioSourceNode。但是由于您关闭了上下文并创建了一个新上下文,这是 Chrome.

中的另一个不同的错误