播放声音而无需安装媒体播放器扩展 [英] playing sound without the need to install media player extension

查看:95
本文介绍了播放声音而无需安装媒体播放器扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在拍卖网站上嵌入声音。我希望每次有人出价,都会播放声音。我尝试使用给定ID的简单< embed> 标记,然后通过javascript尝试 sound.play()。 / p>

声音正在播放,但在IE中,新用户被要求安装媒体播放器扩展如果他/她信任该网站。



有没有办法让它在没有这条消息的情况下播放。以下代码位于页面中:



在HTML中:

 < embed src =success.wavautostart =falsewidth =0height =0id =sound1
enablejavascript =true>

在Javascript中:

  function PlaySound(soundObj){

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

以及对函数的调用:

  PlaySound(sound1); 


解决方案

如果您觉得要求用户拥有Flash ,我建议 SoundManager2 。它在后台使用Flash,但完全通过JavaScript配置,并且可以自定义。



如果你想在浏览器中完全原生地使用它,你可以使用< audio> 标签,但您会发现它只适用于最新的浏览器,并且您仍会遇到一些跨浏览器问题。 这篇Ajaxian文章概述了如何操作以及使用哪些浏览器支持什么音频格式。



PS个人意见,如果你不介意:不要先询问用户,不要播放背景音或音乐。永远不要自动播放并始终为用户提供停止或静音的选项。


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

解决方案

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.

这篇关于播放声音而无需安装媒体播放器扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆