PinkElephantsOnParade
PinkElephantsOnParade

Reputation: 6592

Web Audio API - loading a file and playing it

There are some questions similar on SO but it appears the syntax is all deprecated.

I'm trying to load a sound off of my DropBox (public link) and play it at startup of my app.

Here is my code:

var context = new webkitAudioContext();
function start() {
// Note: this will load asynchronously
var request = new XMLHttpRequest();
request.open("GET", "https://dl.dropboxusercontent.com/u/86304379/Pt.%205%20Flood%20the%20Soul.wav", true);
request.responseType = "arraybuffer"; // Read as binary data
// Asynchronous callback
request.onload = function() {
    var data = request.response;

    audioRouting(data);
};
request.send();
}
function audioRouting(data) {
    source = context.createBufferSource(); // Create sound source
    buffer = context.createBuffer(data, true/* make mono */); // Create source buffer from raw binary
    source.buffer = buffer; // Add buffered data to object
    source.connect(context.destination); // Connect sound source to output
    playSound(source); // Pass the object to the play function
}
    // Tell the Source when to play
function playSound() {
    source.noteOn(context.currentTime); // play the source immediately
}

Alas - I call playSound() in one of my functions and I get

"Cannot call method 'noteOn' of undefined "

Is this because noteOn is deprecated? Or is something else amiss. I just want to load some audio into an audio node and play it - I have not found any ample code online that displays how to do this.

Upvotes: 0

Views: 1733

Answers (2)

OJay
OJay

Reputation: 1239

The above is correct, noteOn is deprecated. But because we cannot be sure if a user will visit with an older browser that only supports deprecated calls or a new browser that only supports new calls, SoundJS sets up compatibility for both by mapping the calls. Check out WebAudioPlugin._compatibilitySetUp if your interested.

Upvotes: 0

phenomnomnominal
phenomnomnominal

Reputation: 5515

You're right, the noteOn function is indeed gone, now it is start:

user:        crogers
date:        Tue Sep 25 12:56:14 2012 -0700
summary:     noteOn/noteOff changed to start/stop -- added deprecation notes

The API is as good as you will get for the docs - the AudioBufferSourceNode section is the bit you want for this.

Here's an example of how I do it (excuse the CoffeeScript).

Upvotes: 3

Related Questions