gpt4 book ai didi

javascript - Web Audio Api 问题(DOM 异常 12)

转载 作者:行者123 更新时间:2023-11-30 06:35:02 28 4
gpt4 key购买 nike

我在带有音频 API 的 Chrome 上发出了奇怪的错误(SYNTAX_ERR:DOM 异常 12)。我第一次尝试 Audio Api 并做了 Kyle Nau( http://www.youtube.com/watch?v=1wYTkZVQKzs ) 的教程(几次)。当我使用简单的 mp3 播放代码时,所有声音都播放正常,但是当我尝试从同一教程添加音量控制 block 时,只播放新对象创建列表中的最后一个声音。两个首先显示“SYNTAX_ERR:DOM Exception 12”。我检查了 mp3 并改变了声明的位置 = 同样的坏效果。删除音量控制,所有播放再次正常。在本教程中也很好。

测试表明,取消注释这部分时会出现问题:

        playSound.connect(this.gainNode);
this.gainNode.connect(audioContext.destination);

我不明白为什么会出现这个错误。

这里是代码。这是很好的工作变体(我用评论标记了问题所在):

    function Sound(source, level) {
if (!window.audioContex) {
audioContext = new webkitAudioContext;
};
var that = this;
that.source = source;
that.buffer = null;
that.isLoaded = false;

//that.gainNode = audioContext.createGain();

//如果(!级别){

//that.gainNode.gain.value = 1;

//} 否则 {

//that.gainNode.gain.value = level;

//};

    var getSound = new XMLHttpRequest();
getSound.open("GET",that.source,true);
getSound.responseType = "arraybuffer";
getSound.onload = function() {
audioContext.decodeAudioData(getSound.response,function(buffer) {
that.buffer = buffer;
that.isLoaded = true;
});
};
getSound.send();
};

Sound.prototype.play = function(){
if(this.isLoaded === true) {
var playSound = audioContext.createBufferSource();
playSound.buffer = this.buffer;

//playSound.connect(this.gainNode);

//this.gainNode.connect(audioContext.destination);

        playSound.connect(audioContext.destination);
playSound.noteOn(0);
};
};

//Sound.prototype.setVolume = function(level) {

//this.gainNode.gain.value = level;

//};

var laserSound = new Sound("sound/laser.mp3");
var dropSound = new Sound("sound/drop.mp3");
var pickupSound = new Sound("sound/pickup.mp3");

//laserSound.setVolume(.1);

window.addEventListener("keydown", onKeyDown);

function onKeyDown(event) {
switch (event.keyCode) {
//Z
case 90:
laserSound.play();
break;
//X
case 88:
dropSound.play();
break;
//C
case 67:
pickupSound.play();
break;
};

};

最佳答案

当您在注释掉的第一行中创建增益节点时,它必须是 audioContext.createGainNode();而不是 audioContext.createGain();

看来您缺少节点。

希望对您有所帮助。

关于javascript - Web Audio Api 问题(DOM 异常 12),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15135868/

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