Javascript chrome音频分析仪断开音频开关

Javascript chrome音频分析仪断开音频开关,javascript,jquery,audio,html5-audio,webkitaudiocontext,Javascript,Jquery,Audio,Html5 Audio,Webkitaudiocontext,我正在用webgl创建一个音频可视化工具,并将soundcloud曲目集成到其中。我不想切换曲目,但我可以让我的可视化工具工作并中断音频,或者让音频工作并中断可视化工具 我有两种方法可以让它工作 音频工作 删除音频元素 将新的音频元素附加到主体 扳机游戏 可视化工作 停止音频 变更来源 扳机游戏 当我让可视化工具工作时,音频完全混乱了。缓冲区听起来不对,而且音频中存在瑕疵(噪音、嘟嘟声和咯咯声) 当音频工作时,当我调用Analyzer.getByteFrequencyData时,我得到一个0的

我正在用webgl创建一个音频可视化工具,并将soundcloud曲目集成到其中。我不想切换曲目,但我可以让我的可视化工具工作并中断音频,或者让音频工作并中断可视化工具

我有两种方法可以让它工作

音频工作

  • 删除音频元素
  • 将新的音频元素附加到主体
  • 扳机游戏

  • 可视化工作

  • 停止音频
  • 变更来源
  • 扳机游戏
  • 当我让可视化工具工作时,音频完全混乱了。缓冲区听起来不对,而且音频中存在瑕疵(噪音、嘟嘟声和咯咯声)

    当音频工作时,当我调用Analyzer.getByteFrequencyData时,我得到一个0的数组。我想这是因为分析仪没有正确连接

    音频工作的代码如下所示

    $('#music').trigger("pause");
    currentTrackNum = currentTrackNum + 1;
    var tracks = $("#tracks").data("tracks")
    var currentTrack = tracks[parseInt(currentTrackNum)%tracks.length];
    // Begin audio switching
    analyser.disconnect();
    $('#music').remove();
    $('body').append('<audio id="music" preload="auto" src="'+ currentTrack["download"].toString() + '?client_id=4c6187aeda01c8ad86e556555621074f"></audio>');
    startWebAudio(),
    
    startweaudio
    函数如下所示

    function startWebAudio() {
      // Get our <audio> element
      var audio = document.getElementById('music');
      // Create a new audio context (that allows us to do all the Web Audio stuff)
      var audioContext = new webkitAudioContext();
      // Create a new analyser
      analyser = audioContext.createAnalyser();
      // Create a new audio source from the <audio> element
      var source = audioContext.createMediaElementSource(audio);
      // Connect up the output from the audio source to the input of the analyser
      source.connect(analyser);
      // Connect up the audio output of the analyser to the audioContext destination i.e. the speakers (The analyser takes the output of the <audio> element and swallows it. If we want to hear the sound of the <audio> element then we need to re-route the analyser's output to the speakers)
      analyser.connect(audioContext.destination);
    
      // Get the <audio> element started  
      audio.play();
      var freqByteData = new Uint8Array(analyser.frequencyBinCount);
    }
    
    函数startWebAudio(){
    
    //获取我们的

    您只能为每个窗口创建一个
    音频上下文
    。您还应该在使用完
    MediaElementSource
    后断开它的连接

    下面是我用来回答类似问题的一个例子:

    function startWebAudio() {
      // Get our <audio> element
      var audio = document.getElementById('music');
      // Create a new audio context (that allows us to do all the Web Audio stuff)
      var audioContext = new webkitAudioContext();
      // Create a new analyser
      analyser = audioContext.createAnalyser();
      // Create a new audio source from the <audio> element
      var source = audioContext.createMediaElementSource(audio);
      // Connect up the output from the audio source to the input of the analyser
      source.connect(analyser);
      // Connect up the audio output of the analyser to the audioContext destination i.e. the speakers (The analyser takes the output of the <audio> element and swallows it. If we want to hear the sound of the <audio> element then we need to re-route the analyser's output to the speakers)
      analyser.connect(audioContext.destination);
    
      // Get the <audio> element started  
      audio.play();
      var freqByteData = new Uint8Array(analyser.frequencyBinCount);
    }