如何使用javascript停止声音
我的html文件中有一个声音文件。我想在使用javascript 30秒后停止播放,下面是要播放的代码如何使用javascript停止声音,javascript,Javascript,我的html文件中有一个声音文件。我想在使用javascript 30秒后停止播放,下面是要播放的代码 var snd = new Audio("sound location"); snd.play(); 有什么帮助吗?您可以: setTimeout(function() { snd.pause(); }, 30000); HTML5音频元素有一些方法,您应该了解 audioElement.pause(); // Will pause the audio audioElement.
var snd = new Audio("sound location");
snd.play();
有什么帮助吗?您可以:
setTimeout(function() {
snd.pause();
}, 30000);
HTML5音频元素有一些方法,您应该了解
audioElement.pause(); // Will pause the audio
audioElement.play(); // Will play the audio
audioElement.volume = 0.5; // Will set the volume to half of max
audioElement.load(); // Will start to load the media
audioElement.currentTime = 0; // Will set the time of the audio to 0
如果你不想停止那里的音频,你可以这样做
audioElement.pause();
audioElement.currentTime = 0;
因此,要回答您的问题,您可以使用设置超时
和停止方式
window.setTimeout(function(){
audioElement.pause();
audioElement.currentTime = 0;
}, 30 * 1000);
查看并使用snd.pause功能
snd.pause
将通过window
作为this
调用,如果通过这种方式。也许snd.pause.bind(snd)可以工作
。或者干脆setTimeout(snd.pause,30000)
?不,@Derek朕會功夫 那是行不通的。我认为答案很简单。@Pinocchio-为什么不呢?因为在pause函数中,它会将snd称为this,如果您这样做,它将被超时窗口更改
window.setTimeout(function(){
audioElement.pause();
audioElement.currentTime = 0;
}, 30 * 1000);