使用 javascript onClick 播放 mp3 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10766538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Play mp3 file with javascript onClick
提问by SopheakVirak
I am playing mp3 file just javascript onClick.
我正在播放 mp3 文件只是 javascript onClick。
Bellow is my code:
波纹管是我的代码:
//Music File 1
<audio id="id1" src="01.mp3"></audio>
<button onClick="document.getElementById("id1").play()">Play</button>
<button onClick="document.getElementById("id1").pause()">Stop</button>
//Music File 2
<audio id="id2" src="02.mp3"></audio>
<button onClick="document.getElementById("id2").play()">Play</button>
<button onClick="document.getElementById("id2").pause()">Stop</button>
My question is, I am playing music 01.mp3 and I want it stop play if I press play button at music 02.mp3.
我的问题是,我正在播放音乐 01.mp3,如果我按下音乐 02.mp3 的播放按钮,我希望它停止播放。
Can any one give me a better solution than this? Totally, I want to way to play music like www.mp3skull.com, other stop play if pressed other file to play.
谁能给我一个比这更好的解决方案?完全,我想像 www.mp3skull.com 这样的播放音乐的方式,如果按下其他文件播放,其他停止播放。
Regards, Virak
问候, 维拉克
回答by Death
Aside from the tag errors (presuming it's just a typo in this writeup), if you want a nicer Javascript audio player, check out http://jplayer.org/- it's an open source, free player API.
除了标签错误(假设它只是本文中的一个错字),如果您想要一个更好的 Javascript 音频播放器,请查看http://jplayer.org/- 它是一个开源的免费播放器 API。
One of the methods available for the player, .pauseOthers()
halts all players on the page other than the one you ran the method on - useful in your situation.
玩家可用的方法之一,.pauseOthers()
停止页面上的所有玩家,而不是您运行该方法的那个 - 在您的情况下很有用。
回答by Marc B
You've got html/javascript syntax errors:
你有 html/javascript 语法错误:
<button onClick="document.getElementById("id2").play()">Play</button>
^ ^ ^ ^
1 2 3 4
quote #2 closes quote #1, so your id2 is OUTSIDE of the onclick, and is part of the button tag. Ditto for 3 & 4. Change the inner quotes to 'id2'
instead.
引用 #2 关闭引用 #1,因此您的 id2 位于 onclick 之外,并且是按钮标记的一部分。3 和 4 同上。将内部引号'id2'
改为。