网络音频API未定义的方法createBuffer和createBufferSource [英] Web Audio API Undefined method createBuffer and createBufferSource

查看:2198
本文介绍了网络音频API未定义的方法createBuffer和createBufferSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Web AudioContext加载音频,但它总是说没有定义createBuffer和createBufferSource方法,我在网上搜索,这确实是有这两种方法,那么为什么我不能给他们打电话?

  VAR背景=新webkitAudioContext();
audioBuffer = context.createBuffer(request.response,真);
无功源= context.createBufferSource();


解决方案

请尝试以下方法,它为我工作:

 的window.onload =的init();功能的init()
{
如果(typeof运算AudioContext ==功能){
    VAR audioContext =新AudioContext();
}否则如果(typeof运算webkitAudioContext ==功能){
    VAR audioContext =新webkitAudioContext();
}无功源= audioContext.createBufferSource();
source.connect(audioContext.destination);VAR XHR =新XMLHtt prequest();
xhr.open(GET,音/ human.mp3,真正的);
xhr.responseType =arraybuffer;
xhr.onload =功能(){
    VAR缓冲= audioContext.createBuffer(xhr.response,FALSE);
    source.buffer =缓冲;
    source.noteOn(0);
};
xhr.send();
}

I am trying to load an audio using web AudioContext, but it always says createBuffer and createBufferSource methods are not defined, and I search online, it indeed has these two methods, then why can't I call them?

var context = new webkitAudioContext();
audioBuffer = context.createBuffer(request.response, true);
var source = context.createBufferSource();

解决方案

Try the following, it works for me:

window.onload = init();

function init ()
{
if (typeof AudioContext == "function") {
    var audioContext = new AudioContext();
} else if (typeof webkitAudioContext == "function") {
    var audioContext = new webkitAudioContext();
}

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

var xhr = new XMLHttpRequest();
xhr.open("GET", "sounds/human.mp3", true);
xhr.responseType = "arraybuffer";
xhr.onload = function() {
    var buffer = audioContext.createBuffer(xhr.response, false);
    source.buffer = buffer;
    source.noteOn(0);
};
xhr.send();
}

这篇关于网络音频API未定义的方法createBuffer和createBufferSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆