Web 音频 API 问题(DOM 异常 12)

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

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

//      if (!level) {

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

//      } else {

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

    };
javascript google-chrome html5-audio
2个回答
1
投票

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

看来您缺少节点。

希望有帮助。


-1
投票

您在某处有语法错误。您不需要在函数声明后添加分号。在这种情况下,您只需使用分号:

var myFunction = function(){

};
© www.soinside.com 2019 - 2024. All rights reserved.