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

拜拜、爱过 提交于 2019-11-30 18:31:49

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!

ebidel

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);

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!