Tin*_*tar 6 javascript html5-audio web-audio-api
这种交换的一个方向是可能的 - 我知道您可以使用元素的源<audio>作为获取音频的一种方式,以便通过createElementSourceNode(). 是否可以做相反的事情,即AudioBufferSourceNode使用 an 作为元素的源<audio>?
我很确定这是不可能的,但我一直在通过 npm 寻找标准的、简洁的默认浏览器音频元素播放控件的娱乐版本,AudioBuffers但我没有找到任何东西。
您可以将 AudioBufferSourceNode 连接到MediaStreamDestination 节点,然后使用该节点的属性提供HTMLAudioElementsrcObject.stream:
start.onclick = e => {
const audioCtx = new AudioContext();
fetch("https://dl.dropboxusercontent.com/s/1cdwpm3gca9mlo0/kick.mp3")
.then(resp => resp.arrayBuffer())
.then(buf => audioCtx.decodeAudioData(buf))
.then(audioBuffer => {
const source = audioCtx.createBufferSource();
source.buffer = audioBuffer;
source.playbackRate.value = 0.1;
source.loop = true;
source.start(0);
const streamNode = audioCtx.createMediaStreamDestination();
source.connect(streamNode);
const audioElem = new Audio();
audioElem.controls = true;
document.body.appendChild(audioElem);
audioElem.srcObject = streamNode.stream;
})
.catch(console.error);
}Run Code Online (Sandbox Code Playgroud)
<button id="start">start</button>Run Code Online (Sandbox Code Playgroud)