Javascript在悬停时播放声音。停止并在关闭时重置
这是我播放音频文件的代码Javascript在悬停时播放声音。停止并在关闭时重置,javascript,audio,onmouseover,onmouseout,onhover,Javascript,Audio,Onmouseover,Onmouseout,Onhover,这是我播放音频文件的代码 function EvalSound(soundobj) { var thissound=document.getElementById(soundobj); thissound.currentTime = 0; thissound.Play(); } function StopSound(soundobj) { var thissound=document.getElementById(soundobj); thissou
function EvalSound(soundobj) {
var thissound=document.getElementById(soundobj);
thissound.currentTime = 0;
thissound.Play();
}
function StopSound(soundobj) {
var thissound=document.getElementById(soundobj);
thissound.Stop();
}
它目前正在使用hover,但是当我返回到它在其下播放的图像时,它不会返回到开头,它会继续播放标签是嵌入多媒体的旧方法。你真的应该使用新的HTML5来通过媒体播放、暂停和搜索(以及更多)
这是一个在mouseover上播放音频文件并在mouseout上停止播放的程序
HTML:
有关更多信息,请查看我有同样的问题,关于启动和停止音频。我使用jQuery时没有任何其他插件。我的代码用于mousedown和mouseup,但可以更改为其他操作 HTML
函数stopAudio(){
player.pause();
player.currentTime=0;
}
停止
//结束说明:您必须查看您的音频id,因为我的名为“播放器”
这就是它不起作用的原因看看你的css和html
非常接近你的路线。还有一件事你必须小心
您的调用函数是否与我的一样声明为stopAudio(),您的可能是
命名不同。该函数仅在使用调用时与css一起使用
方法正确。您是如何主持您的声音的?我以前也遇到过同样的问题,因为python的SimpleHTTPServer中有一个奇怪的bug…只是别忘了给用户关闭声音的选项:-)我刚刚像这样嵌入了文件,我建议将
thissound.currentTime=0代码>在StopSound()而不是EvalSound()中。这样,如果您确实需要暂停声音,EvalSound函数仍将按预期工作。函数EvalSound(soundobj){var thissound=document.getElementById(soundobj);thissound.currentTime=0;thissound.Play();}函数StopSound(soundobj){var thissound=document.getElementById(soundobj);thissound.Stop();}函数EvalSound(soundobj){var thissound=document.getElementById(soundobj)}(soundobj);thissound.Play();}函数StopSound(soundobj){var thissound=document.getElementById(soundobj);this.currentTime=0;this.pause();}啊,你在使用标签。我真的建议切换到标签。这是将音频嵌入网页的首选方式。我在上面的回答中添加了一些示例代码。
onmouseover="EvalSound('sound1')" onmouseout="StopSound('sound1')"
<p onmouseover="PlaySound('mySound')"
onmouseout="StopSound('mySound')">Hover Over Me To Play</p>
<audio id='mySound' src='http://upload.wikimedia.org/wikipedia/commons/6/6f/Cello_Live_Performance_John_Michel_Tchaikovsky_Violin_Concerto_3rd_MVT_applaused_cut.ogg'/>
function PlaySound(soundobj) {
var thissound=document.getElementById(soundobj);
thissound.play();
}
function StopSound(soundobj) {
var thissound=document.getElementById(soundobj);
thissound.pause();
thissound.currentTime = 0;
}
<div class="soundbutton">
cool wind sound
</div>
<audio id="wind-sound" src="wind-sound/wind.mp3">
$('.soundbutton').on('mousedown', function () {
playWind(); //start wind sound
})
.on('mouseup', function () {
stopWind(); //stops the wind sound
});
// my full functions to start and stop
function playWind () { //start wind audio
$('#wind-sound')[0].volume = 0.7;
$('#wind-sound')[0].load();
$('#wind-sound')[0].play();
}
function stopWind () { //stop the wind audio
$('#wind-sound')[0].pause();
$('#wind-sound')[0].currentTime = 0; //resets wind to zero/beginning
}
<html>
<script>
function stopAudio() {
player.pause();
player.currentTime = 0;
}
</script>
<body>
<audio id="player" src="(place audio here)"></audio>
<div>
<button onclick=" stopAudio()">Stop</button>
</div>
</body>
</html>