views:

258

answers:

2

Hello,

I am trying to embed a sound in an auction website. I want that everytime someone's bid, a sound will be played. I tried a simple tag with given ID, and then sound.play() through javascript.

The sound is playing, but in IE, a new user is asked to install a media player extension "if he/she trusts the website".

Is there a way to make it play without this message. The following code is in the page:

In the HTML:

<embed src="success.wav" autostart="false" width="0" height="0" id="sound1" enablejavascript="true">

In the Javascript:

function PlaySound(soundObj) {

  var sound = document.getElementById(soundObj);
  sound.Play();
}

And the call to the function:

PlaySound("sound1");

Any Ideas are welcome !

+1  A: 

If you're comfortable requiring the user to have Flash, I recommend SoundManager2. It uses Flash in the background but is configured entirely through JavaScript, and it's very customizable.

If you want to do it completely natively in the browser you can use the <audio> tag, but you'll find it only works in the very newest browsers, and there are still some cross-browser issues you're going to hit upon. This Ajaxian article gives a good overview of how to do it and which browsers support what audio formats.

P.S. A personal opinion, if you don't mind: Don't play background sounds or music without first asking the user. Never autoplay and always give the user the option to stop or mute the sound.

Jordan
Hi thank you for the answer.This soundmanager looks promising. I hope it's not too heavy or have conflicts with jquery... about the <audio> tag - point is that I need the sound even play in IE6... so I am not sure I want to get in to cross browser issues :(You right about the auto-play. ThanksThanks again for the answer!
OfficeJet
Your concerns are valid. Probably wrapping Flash with JavaScript (as with SoundManager2) is going to be your best bet for backwards compatibility. I don't think SM2 conflicts with jQuery, but it's something you'll have to evaluate. FWIW, the SM2 site says that with gzip enabled the "minified" version is "as light as 6 KB": http://www.schillmania.com/projects/soundmanager2/doc/getstarted/
Jordan
+1  A: 

Well the solution worked great with sound manager 2. Altough the whole installation file is 2.8mb, I think only small part of it actually used.

What I did is put the following code in the header:

<script type="text/javascript">

soundManager.url = '<?php echo $root_address; ?>/js/soundmanager/swf/'; 
soundManager.debugMode = false;

soundManager.onload = function() {
  var mySound = soundManager.createSound({
    id: 'aSound',
    url: '<?php echo $root_address; ?>/success.mp3'
  });
}
</script>

And replaced the calling to the play sound function with this code:

soundManager.play('aSound');

I used the simplest API to do it.

Thanks again!

OfficeJet