gpt4 book ai didi

javascript - Javascript-即时串流音频(Web Audio API和XHR)

转载 作者:行者123 更新时间:2023-12-03 01:48:36 25 4
gpt4 key购买 nike

我有一个运行简单的xmlhttprequest来获取音频文件,完成获取后,它将解码并播放音频。

        var xhr = new XMLHttpRequest();
xhr.open('GET', /some url/, true);
xhr.responseType = 'arraybuffer';
xhr.onload = function() {
decode(xhr.response);
}.bind(this);
xhr.send(null);

但是,这样做的问题是,仅在请求被加载/完成下载后文件才解码。是否有一种无需等待 <audio>标记即可流传输音频而无需等待下载完成的方法?

最佳答案

您仍然需要HTML5 Audio对象,但是与其直接使用它而不是直接使用它,还可以使用MediaElementAudioSourceNodeAudio元素来利用Web API。

摘自here

Rather than going the usual path of loading a sound directly by issuing an XMLHttpRe quest and then decoding the buffer, you can use the media stream audio source node (MediaElementAudioSourceNode) to create nodes that behave much like audio source nodes (AudioSourceNode), but wrap an existing tag. Once we have this node connected to our audio graph, we can use our knowledge of the Web Audio API to do great things. This small example applies a low-pass filter to the tag:



示例代码:
window.addEventListener('load', onLoad, false);

function onLoad() {
var audio = new Audio();
source = context.createMediaElementSource(audio);

var filter = context.createBiquadFilter();
filter.type = filter.LOWPASS;
filter.frequency.value = 440;

source.connect(this.filter);
filter.connect(context.destination);

audio.src = 'http://example.com/the.mp3';
audio.play();
}

关于javascript - Javascript-即时串流音频(Web Audio API和XHR),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41591647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com