0

I need to make a sound from a webpage immediately after load (OK/NOT OK signal depending on the case). The page is generated and I can control the content fully.

How do I do this in a modern, cross browser compatible way? I've experienced problems with <audio> tag (maybe browser issues, maybe I'm doing it somehow wrong). Currently I use a small flash player, but as you might guess, it is not a perfect solution.

And yes, the sound is exactly what the user wants, so please no "website with sound is not a good idea" -comments. Generally I would agree, but there are special cases.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
Tero Lahtinen
  • 514
  • 6
  • 16

2 Answers2

0

Using an audio element works in modern browsers, and for older browsers, you can use an embed fallback (which may or may not work, depending on installed plugins, but if it does not work, there is not much you can do):

<audio src=maamme.mp3 controls autoplay>  
  <embed src=maamme.mp3>
</audio>

This creates visible controls at the place where you put this element. You can modify those controls to some extent or hide them. If you want to control more exactly when the presentation starts, you can dynamically add the element into the document instead of having it statically there. If old browsers are not very relevant, you could alternatively use an audio element without autoplay and use the HTMLMediaElement interface to start the presentation, do things when it has ended, etc.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
0

If you wish to control the audio yourself, you can do it programatically through javascript.

window.onload = function(){
    var snd = new Audio("sound/mysound.wav");
    snd.play();
}

This should load the audio file and play it automatically once the page has loaded. It should be noted however that iOS limits any audio being played like this, without a user interaction (e.g. a click) because it forces the user to use up bandwidth and takes control away from them. Android, and other devices may or may not allow autoplaying audio, but all latest web browsers on desktop allow it (Chrome, FF, IE9+, Safari)

Jonathon Blok
  • 749
  • 4
  • 14