我在带有音频API的Chrome上发出了奇怪的错误(SYNTAX_ERR:DOM异常12)。我第一次尝试了Audio Api,并做了Kyle Nau(http://www.youtube.com/watch?v=1wYTkZVQKzs)的教程(几次)。当我使用简单的mp3运行代码时,所有声音播放都很好,但是当我尝试从同一教程中添加音量控制块时,只会播放新对象创建列表中的最后一个声音。首先播放两个节目“ SYNTAX_ERR:DOM异常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();

//如果(!level){

// 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;
    };

};

最佳答案

您在某处存在语法错误。您无需在函数声明后放置半冒号。在这种情况下,您只能使用半冒号:

var myFunction = function(){

};

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

10-10 20:47