Uncaught TypeError: Value is not of type AudioBuffer
Asked Answered
R

2

2

I get this error when I try to run an XHR to load a sample. Uncaught TypeError: Value is not of type AudioBuffer. Everything seems to be right, but I'm not sure what the problem is.

Kit.prototype.load = function(){
    if(this.startedLoading)
        return;
    this.startedLoading = true;

    // var kick = "samples/M-808Sn2.wav";
    var snare = "samples/M-808Sn2.wav";
    // var hihat = "samples/M-808Sn2.wav";

    // this.loadSample(0, kick, false);
    this.loadSample(1, snare, false);
    // this.loadSample(2, hihat, false);
}

I start it off with the request:

Kit.prototype.loadSample = function(sampleID, url, mixToMono){
// Load Asynchronously

var request = new XMLHttpRequest();
request.open("GET", url, true);
request.responseType = "arraybuffer";

var kit = this;

request.onload = function(){

    var buffer;
    context.decodeAudioData(request.response, function(decodedBuffer){
        buffer = decodedBuffer;
    });

    switch(sampleID){
        // case 0: kit.kickBuffer = buffer; break;
        case 1: kit.snareBuffer = buffer; break;
        // case 2: kit.hihatBuffer = buffer; break;
    }
}

request.send();

}

Then I try to run it.

 context = new webkitAudioContext();


var kit = new Kit();

kit.load();

var voice = context.createBufferSource();

voice.buffer = kit.snareBuffer;
voice.loop = true;
voice.playbackRate.value = 1;
voice.connect(gain);

voice.start(0);
voice.stop(2);
Recapture answered 12/2, 2013 at 3:42 Comment(0)
T
2

The issue is that kit.load() fires off an asynchronous request to load and decode the audio buffers. The two lines that follow (createBufferSource, assign the buffer) follow the asynchronous request immediately, whether or not the files have been loaded. So the reason you're getting this "Value is not of type AudioBuffer" is because at the time you assign voice.buffer = kit.snareBuffer, kit.snareBuffer is still undefined because the load callback hasn't fired.

The reason this probably works with window.onload is because the XHR request can run before the window has loaded, which means that by the time the window.onload event handler is fired, the XHR request has already come back and kit.snareBuffer is a defined audioBuffer.

This answer is coming a little late, but hope it helps!

Tompion answered 13/6, 2013 at 13:21 Comment(0)
A
0

Looks like a regression in chrome. Even I am getting same error. See below chrome bug

http://code.google.com/p/chromium/issues/detail?id=128826

Apollonius answered 4/3, 2013 at 11:53 Comment(1)
wrapped it in an window.onload = init; that fixed it for me.Recapture

© 2022 - 2024 — McMap. All rights reserved.