Adam Bell
Adam Bell

Reputation: 1045

Can't get audio to play in my Flash / HTML5 / Canvas project

Having lots of problems getting my sounds to play in a Flash-based Canvas project. I'm using the base example here (http://createjs.com/Docs/SoundJS/classes/Sound.html) in order work and all I get is a black screen and the following errors:

SyntaxError: missing ) after argument list

Which in my JS file is the following:

createjs.Sound.on("fileload", createjs.proxy(this.loadHandler, (this));

So I add the extra ) but now another error:

TypeError: createjs.Sound is undefined

Which is this:

createjs.Sound.registerPlugins([createjs.WebAudioPlugin, createjs.FlashAudioPlugin]);

What's going on here? Why can't I just get this to play?

Here's the complete code:

createjs.Sound.registerPlugins([createjs.WebAudioPlugin, createjs.FlashAudioPlugin]);
createjs.Sound.alternateExtensions = ["mp3"];
createjs.Sound.on("fileload", createjs.proxy(this.loadHandler, (this)));
createjs.Sound.registerSound("sounds/LaGrange.mp3", "sound");
function loadHandler(event) {
     // This is fired for each sound that is registered.
     var instance = createjs.Sound.play("sound");  // play using id.  Could also use full source path or event.src.
     instance.on("complete", createjs.proxy(this.handleComplete, this));
     instance.volume = 0.5;
}

Upvotes: 0

Views: 1768

Answers (2)

OJay
OJay

Reputation: 1239

Well that's embarrassing. The example does have 2 errors. The first is the missing ")", which you caught and corrected. The second is that the FlashAudioPlugin is not completely setup. You need to add this line to set the path to the FlashAudioPlugin.swf before you register the plugins:

 createjs.FlashAudioPlugin.swfPath = "../src/soundjs/flashaudio";

Thanks for catching this. I've pushed up a fixed version of this to github. Also, all the of the demos are well tested if you want more working examples to test from.

Hope that helps.

Upvotes: 2

Lanny
Lanny

Reputation: 11294

If you are testing locally (ie, not on a webserver - remote or local), then WebAudio can not play, since it uses XHR to load it, which does not work cross-domain (using file:/// is considered unsafe as well)

Change it to only use HTMLAudio when testing, and it should work.

Upvotes: 0

Related Questions