Monday, 20 June 2011

Quick Tip - HTML5 Audio for Game

Note: Audio file used in this page is non-existant. Must update this page soon.

I'll just show HTML5 Audio being used with javascript without a canvas. Using it in canvas you just include it in the javascript like normal by calling the function that plays the sound.
The JavaScript accesses the HTML5 audio, and in effect makes an <audio> element. But it's good this way, as it's less cluttered without the tags actually being there or being created.

var boingSound = new Audio("");
function playSound() {
 boingSound.currentTime = 0;;

Notice how it sets the currentTime to 0 before playing. This is because you can't play the same sound object twice without moving the time back to the start again.

Here is a use of the result of that: