playing sound without the need to install media player extension

自作多情 提交于 2019-12-08 12:00:33

问题


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("sound1");

回答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.




回答2:


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!



来源:https://stackoverflow.com/questions/1883262/playing-sound-without-the-need-to-install-media-player-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!