单击图像时播放 MP3 [英] Play MP3 with when an image is clicked
本文介绍了单击图像时播放 MP3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我单击我网站中的某个图像时,我想播放 MP3 文件.我还希望隐藏 MP3 文件.我怎样才能做到这一点?我尝试了这段代码,但没有任何反应:
I want to play an MP3 file when I click a certain image in my site. I also want the MP3 file to be hidden. How can I do that? I tried this code but nothing happens:
<html>
<body>
<script language="javascript" type="text/javascript">
function playSound(soundfile) {
document.getElementById("dummy").innerHTML=document.getElementById("dummy").innerHTML +
"<embed src=""+mp3/a/bat.mp3+"" hidden="true" autostart="true" loop="false" />";
}
<span id="dummy" onclick="playSound('mp3/a/bat.mp3');"><img src="short_a/bat.jpg"
name="Bottom-1" width="115" height="45" border="0" id="Bottom-1"/></a></span>
</script>
</body>
</html>
推荐答案
这与 PHP
无关,您只需将 HTML
代码添加到您的 PHP
页面.
This is not about PHP
, you will just add HTML
code to your PHP
page.
如果你坚持嵌入标签,这里就是你想要的代码,DEMO
If you're insistent on embed tag here is the code you want, DEMO
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function playSound(el,soundfile) {
var embed = document.getElementById("embed");
if (!embed) {
var embed = document.createElement("embed");
embed.id= "embed";
embed.setAttribute("src", soundfile);
embed.setAttribute("hidden", "true");
el.appendChild(embed);
} else {
embed.parentNode.removeChild(embed);
}
}
</script>
</head>
<body>
<span id="dummy" onclick="playSound(this, 'https://dl.dropbox.com/u/39640025/MP3/Waves.mp3');">
<img src="short_a/bat.jpg" name="Bottom-1" width="115" height="45" border="0" id="Bottom-1"/>
</span>
</body>
</html>
不过我还是推荐你使用音频 API,DEMO
Nevertheless I recommend you use Audio API, DEMO
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function playSound(el,soundfile) {
if (el.mp3) {
if(el.mp3.paused) el.mp3.play();
else el.mp3.pause();
} else {
el.mp3 = new Audio(soundfile);
el.mp3.play();
}
}
</script>
</head>
<body>
<span id="dummy" onclick="playSound(this, 'https://dl.dropbox.com/u/39640025/MP3/Waves.mp3');">
<img src="short_a/bat.jpg" name="Bottom-1" width="115" height="45" border="0" id="Bottom-1"/>
</span>
</body>
</html>
这篇关于单击图像时播放 MP3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文