开发者

playing sound without the need to install media player extension

I am trying to embed a sound in an auction website. I want that every time someone's bid, a sound will be played. I tried a simple <embed> 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("sound开发者_C百科1");


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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜