Load audiodata into AudioBufferSourceNode from <audio/> element via createMediaElementSource?

前端 未结 3 911
攒了一身酷
攒了一身酷 2021-01-04 06:23

Is it possible to have an audiofile loaded from -element via createMediaElementSource and then load the audio data into a Aud

相关标签:
3条回答
  • 2021-01-04 06:33

    This is possible. See my post at http://updates.html5rocks.com/2012/02/HTML5-audio-and-the-Web-Audio-API-are-BFFs. There is also a code snippet and example there. There are a few outstanding bugs, but loading an <audio> into the Web Audio API should work as you want.

    // Create an <audio> element dynamically.
    var audio = new Audio();
    audio.src = 'myfile.mp3';
    audio.controls = true;
    audio.autoplay = true;
    document.body.appendChild(audio);
    
    var context = new webkitAudioContext();
    var analyser = context.createAnalyser();
    
    // Wait for window.onload to fire. See crbug.com/112368
    window.addEventListener('load', function(e) {
      // Our <audio> element will be the audio source.
      var source = context.createMediaElementSource(audio);
      source.connect(analyser);
      analyser.connect(context.destination);
    
      // ...call requestAnimationFrame() and render the analyser's output to canvas.
    }, false);
    
    0 讨论(0)
  • 2021-01-04 06:37

    I'm not sure if you found a better solution yet and I also checked the W3C link you posted: http://www.w3.org/2011/audio/wiki/Spec_Differences#Reading_Data_from_a_Media_Element

    But in order for it to really work you have to use AudioContext.createScriptProcessor(). I didn't yet tried this but basically you connect the source node (an audio element) to a script processor but then don't even output the audio if you don't need it. In the onaudioprocess callback you have direct access to the audio buffer data (in chunks of a specified size of course). There are examples in the link above.

    Also I think you can somehow tweak the speed of the playback so that you can get more buffer arrays faster.

    0 讨论(0)
  • 2021-01-04 06:50

    Seems as if it's not possible to extract the audiobuffer from an MediaElementSourceNode.

    see https://groups.google.com/a/chromium.org/forum/?fromgroups#!topic/chromium-html5/HkX1sP8ONKs

    Any reply proving me wrong is very welcome!

    0 讨论(0)
提交回复
热议问题