使用 JS 加载和播放音频时遇到问题

问题描述 投票:0回答:1

我在这里尝试了加载和播放方法:
http://chromium.googlecode.com/svn/trunk/samples/audio/doc/loading-sounds.html
并编写我自己的 html 和 js 代码,但在加载音频时遇到问题,我总是得到 null

bufferList
arraybuffer
。我不知道为什么,请有人写一个简单的代码或告诉我如何在数组中加载音频,以便我可以使用 websocket 发送它
我不知道如何在这里添加代码,但我的代码与链接中的代码非常相似。

audio websocket web-audio-api
1个回答
0
投票

试试这个:

window.onload = init();

function init ()
{
var audioContext = new webkitAudioContext();

var source = audioContext.createBufferSource();
source.connect(audioContext.destination);

var xhr = new XMLHttpRequest();
xhr.open("GET", "sounds/sample.mp3", true);
xhr.responseType = "arraybuffer";
xhr.onload = function() {
    var buffer = audioContext.createBuffer(xhr.response, false);
    source.buffer = buffer;
    source.noteOn(0);
};
xhr.send();
}
© www.soinside.com 2019 - 2024. All rights reserved.