用javascript播放声音 [英] Play sound with javascript

查看:113
本文介绍了用javascript播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在每行中创建一个带有字符串和按钮(播放声音)的表格,并且每个按钮都会发出不同的声音。

我想用此方法:

 < script> 
函数EvalSound(soundobj){
var thissound = document.getElementById(soundobj);
thissound.Play();
}
< / script>

< embed src =success.wavautostart = false width = 1 height = 1 id =sound1
enablejavascript =true>



这是按钮:

 < form> 
< input type =buttonvalue =播放声音onClick =EvalSound('sound1')>
< / form>

问题是我想在这里:

 < input type =buttonvalue =播放声音onClick =EvalSound('sound1')> 

写入文件的URL而不是'sound1',可以这样做吗?或者我需要更改代码中的其他内容?



编辑:



我构建如下脚本:

 < script> 
函数EvalSound(soundobj){
var embed = document.createElement('embed');
embed.setAttribute('width',1);
embed.setAttribute('height',1);
embed.setAttribute('src',soundobj);
embed.setAttribute('autostart',false);
embed.setAttribute('enablejavascript',true);
embed.Play();
}
< / script>

然后调用它:

 <形式> 
< input type =buttonvalue =播放声音onClick =EvalSound('sound url')>
< / form>

,它仍然不会播放声音。 h2_lin>解决方案

您可以按如下方式实现它: 参考blog 如何在点击或MouseOver上播放声音

  < script language =javascripttype =text / javascript> 
函数playSound(soundfile){
document.getElementById(dummy)。innerHTML =
< embed src = \+ soundfile +\hidden = \ true \autostart = \true \loop = \false \/>;
}
< / script>

在网页中

 < span id =dummy>< / span> 
< a href =#onclick =playSound('URL to soundfile');>点击此处收听声音< / a>


I want to create a table with string and button(that play sound) in each row and every button play a different sound.

i want to do it with this method :

<script>
function EvalSound(soundobj) {
  var thissound=document.getElementById(soundobj);
  thissound.Play();
}
</script>

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

and this is the button:

<form>
<input type="button" value="Play Sound" onClick="EvalSound('sound1')">
</form>

the problem is that i want here :

<input type="button" value="Play Sound" onClick="EvalSound('sound1')">

to write the file URL instead of 'sound1', it is possible to do it in this way? or i need to change another stuff in the code?

Edit :

i build the script like this:

<script>
   function EvalSound(soundobj) {
   var embed = document.createElement('embed');
   embed.setAttribute('width',1);
   embed.setAttribute('height',1);
   embed.setAttribute('src',soundobj);
   embed.setAttribute('autostart', false);
   embed.setAttribute('enablejavascript', true);
   embed.Play();
   }
</script>

and call this with :

<form>
    <input type="button" value="Play Sound" onClick="EvalSound('sound url')">
</form>

and it still not play the sound.

解决方案

You can achieve it as follow:

refer blog How to Play a Sound on Click or on MouseOver

<script language="javascript" type="text/javascript">
 function playSound(soundfile) {
 document.getElementById("dummy").innerHTML=
 "<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
 }
 </script>

In web page

<span id="dummy"></span>
<a href="#" onclick="playSound('URL to soundfile');">Click here to hear a sound</a>

 <p onmouseover="playSound('URL to soundfile');">Mouse over this text to hear a sound</p>

这篇关于用javascript播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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