1
我想通过拼凑示例在线跟随教程。我觉得这应该是播放MP3文件。我使用的是Chrome浏览器,并且它是最新的。我没有在控制台上发现任何错误。我不确定我需要更改或添加这项工作。基本的Web音频API不播放声音
<html>
<head>
<script type="text/javascript">
var context;
var sound1Buffer = null;
var url = 'https://dl.dropboxusercontent.com/u/1957768/SrtV2.mp3';
function init(){
try {
window.AudioContext = window.AudioContext || window.webkitAudioContext;
context = new AudioContext();
}
catch(e) {
alert("web Audio api is not supported!");
}
}
window.addEventListener('load', init, false);
function loadDogSound(url){
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.responseType = 'arrayBuffer';
//decode asynchronously
request.onload = function(){
context.decodeAudioData(request.response, function(buffer){
sound1Buffer = buffer;
}, onError);
}
request.send();
}
function playSound(sound1Buffer){
var source = context.createBufferSource();
source.sound1Buffer = sound1Buffer;
source.connect(context.destination);
source.start(0);
}
</script>
</head>
<body>
</body>
</html>
功能'的init()'没有问题,如果运行的浏览器不支持音频API。这让我觉得所有的函数在加载到浏览器时都会运行。我必须改变'window.addEventListener'中的内容吗?什么是调用'loadDogSound'和'playSound'的正确方法。原谅我,但我对这个东西还很陌生。 – oxxi
嘿,我喜欢搞乱网络音频API。我可以帮你吗?我们聊聊吧? - http://chat.stackoverflow.com/rooms/32365/room-for-uber5001-and-oxxi – uber5001