Javascript HTML音频元素如何重播?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7005472/
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
HTML audio Element how to replay?
提问by Dreampuf
I setup a HTML5 page, that include a node. The page will to playing a music(mp3 format),but it just play sound to end, and I use javascript to control audio element,even so,I can't replay it and only STOP.
我设置了一个 HTML5 页面,其中包含一个节点。该页面将播放音乐(mp3格式),但它只是播放声音结束,我使用javascript来控制音频元素,即使如此,我也无法重播它,只能停止。
Does HTML5 audio Element support REPLAY? And how to?
HTML5 音频元素是否支持重放?以及怎么做?
回答by Pramendra Gupta
please read this
请阅读这个
and for replay event
和重播事件
you can set option, on replayclick
您可以设置选项,在重播点击
audioElement.currentTime=0;
audioElement.play();
回答by Fred Peters
The suggestions above did not worked for me or where not applicable. I needed to replay sound when my code needed to do it.
以上建议对我不起作用或不适用。当我的代码需要这样做时,我需要重放声音。
The following code worked (at least chrome):
以下代码有效(至少是 chrome):
audioElement.setAttribute('src', audioSrc); audioElement.play();
audioElement.setAttribute('src', audioSrc); audioElement.play();
in other words, setting the src attribute again, then play() did the job
换句话说,再次设置 src 属性,然后 play() 完成了这项工作
回答by user7610
If the server hosting the video does not support seeking on client (HTTP RangeRequest), the command
如果托管视频的服务器不支持在客户端上搜索 (HTTP RangeRequest),则命令
audioElement.currentTime=0;
from @JapanPro's answer is silently ignored in Chromium. This behavior is filled as crbug #121765.
来自@JapanPro 的回答在 Chromium 中被默默忽略。此行为填充为crbug #121765。
There is a handy guide on MDN how to configure your serverto play nice. But if you cannot, I had success with doing
在 MDN 上有一个方便的指南如何配置您的服务器以发挥良好的作用。但如果你不能,我已经成功了
audioElement.load();
which unsurprisingly reloads the audio source and seeks to the start as a side effect. Not nice, but it is quite intuitive and it works.
不出所料,它会重新加载音频源并作为副作用寻求开始。不太好,但它非常直观并且有效。
source: http://www.whatwg.org/specs/web-apps/current-work/#loading-the-media-resource
来源:http: //www.whatwg.org/specs/web-apps/current-work/#loading-the-media-resource
回答by samccone
The html5 audio element has a loop attribute set it to loop=loop
html5 音频元素有一个循环属性,将其设置为 loop=loop
回答by ysrb
You can try this:
你可以试试这个:
<audio id="audiotag1" src="audio/flute_c_long_01.wav" preload="auto"></audio>
<a href="javascript:play_single_sound();">Play 5-sec sound on single channel</a>
<script type="text/javascript">
function play_single_sound() {
document.getElementById('audiotag1').play();
}
</script>
回答by nikoloza
don't use node.load()
this is way too slow for slow devices.
不要使用node.load()
这对于慢速设备来说太慢了。
To stop, you should just use:
要停止,您应该使用:
node.currentTime = 0
To replay you can do:
要重播,您可以执行以下操作:
node.currentTime = 0
node.play()
回答by Are Butuv
Use:
用:
audio.pause() // important!!!
audio.currentTime = 0
audio.play()
回答by Pinonirvana
In HTML5 you can just set audioElement.loop='true';
在 HTML5 中,您只需设置 audioElement.loop='true';