无需安装媒体播放器扩展即可播放声音 [英] playing sound without the need to install media player extension
问题描述
我正在尝试在拍卖网站中嵌入声音.我希望每次有人出价时,都会播放声音.我尝试了一个带有给定 ID 的简单 <embed>
标签,然后通过 javascript 尝试了 sound.play()
.
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.
声音正在播放,但在 IE 中,要求新用户安装媒体播放器扩展如果他/她信任该网站".
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:
在 HTML 中:
<embed src="success.wav" autostart="false" width="0" height="0" id="sound1"
enablejavascript="true">
在 Javascript 中:
In the Javascript:
function PlaySound(soundObj) {
var sound = document.getElementById(soundObj);
sound.Play();
}
以及对函数的调用:
PlaySound("sound1");
推荐答案
如果您愿意要求用户拥有 Flash,我推荐 SoundManager2.它在后台使用 Flash,但完全通过 JavaScript 进行配置,并且非常可定制.
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.
如果你想在浏览器中完全原生地做到这一点,你可以使用 <audio>
标签,但你会发现它只适用于最新的浏览器,而且仍然有一些您将遇到的跨浏览器问题.这篇 Ajaxian 文章很好地概述了如何做到这一点以及哪些浏览器支持什么音频格式.
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.
这篇关于无需安装媒体播放器扩展即可播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!