1

I would like to play short sounds in my new JavaScript game. It is a poker game so the sounds are pretty short (dealt card sound, shuffle deck sound, your turn beep etc.)

I Googled before posting here but all I could see was some MP3 players that actually have "play\stop" buttons, (which are not good for me)

Any ideas?

double-beep
  • 5,031
  • 17
  • 33
  • 41
Urbanleg
  • 6,252
  • 16
  • 76
  • 139
  • 1
    I have an idea, keep looking. A search using "javascript sound" turns up hundreds of examples. – Lazarus Dec 27 '11 at 10:21
  • duplicate? http://stackoverflow.com/questions/450033/playing-sound-notifications-using-javascript – rene Dec 27 '11 at 10:26

3 Answers3

2

Put an <audio> element on your page.
Get your audio element and call the play() method:

document.getElementById('yourAudioTag').play();

Check out this example: http://www.storiesinflight.com/html5/audio.html

This site uncovers some of the other cool things you can do such as load(), pause(), and a few other properties of the audio element.

When exactly you want to play this audio element is up to you. Read the text of the button and compare it to "no" if you like.

Alternatively

http://www.schillmania.com/projects/soundmanager2/

SoundManager 2 provides a easy to use API that allows sound to be played in any modern browser, including IE 6+. If the browser doesn't support HTML5, then it gets help from flash. If you want stricly HTML5 and no flash, there's a setting for that, preferFlash=false

It supports 100% Flash-free audio on iPad, iPhone (iOS4) and other HTML5-enabled devices + browsers

Use is as simple as:

<script src="soundmanager2.js"></script>
<script>
// where to find flash SWFs, if needed...
soundManager.url = '/path/to/swf-files/';

soundManager.onready(function() {
    soundManager.createSound({
        id: 'mySound',
        url: '/path/to/an.mp3'
    });

    // ...and play it
    soundManager.play('mySound');
});

Here's a demo of it in action: http://www.schillmania.com/projects/soundmanager2/demo/christmas-lights/

Joris Van Regemortel
  • 935
  • 1
  • 10
  • 34
1

One Word. SoundManager2

Dutchie432
  • 28,798
  • 20
  • 92
  • 109
0

I believe the common way is to make an invisible flash applet (as "sound library") and trigger it to play the sounds appropriately.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194