Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I want to add a short audio track to a game of concentration I'm coding. I want the sound to occur when the user has won the game. How do I go about doing this? I would really appreciate the help, thank you.

share|improve this question
2  
This might help: stackoverflow.com/questions/1933969/… –  bummzack Oct 25 '11 at 7:30
    
In my game, I play music with an embedded Youtube player. –  jco Dec 28 '11 at 13:55
    
What browsers and versions do You want to support? –  Michas Dec 28 '11 at 14:36

4 Answers 4

If you're using something like canvas and you're already depending on the browser supporting HTML5, you can use the audio element.

var win = new Audio('srcfile.wav');
if(playerWon === true){
    win.play();
}
share|improve this answer

Audio in HTML5 is a minefield at the moment, with patchy format support.

We wrote a few blog posts about audio support in HTML5. This chart shows browser support: http://www.scirra.com/blog/44/on-html5-audio-formats-aac-and-ogg#chart

Surprisingly, IE doesn't support playback of WAVs. You should pick audio 2 formats to get coverage on all the platforms.

Beware using MP3's in games

It will cost you $2,500 per title in licensing fees if it has more than 5,000 distributions. http://www.scirra.com/blog/64/why-you-shouldnt-use-mp3-in-your-html5-games

We think the best combination to use is AAC/Ogg to cover all browsers.

share|improve this answer

I found an article describing this library which should fit your needs.

If you don't have sound files yet and want to create them with some fx library then you might want to look at jsfx which I used in my last game typdown (specially these two files are interesting (don't mind the coffeescript, it all compiles to javascript): sounds and game controller

share|improve this answer

The Web Audio API is perfect for playing audio in javascript, but currently only Chrome supports it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.