Play audio and restart it onclick(播放音频并单击重新启动)
问题描述
我希望在 HTML5 音频播放器中重新启动音频文件.我已经定义了一个音频文件和一个 play
按钮.
I'm looking to restart an audio file in a HTML5 audio player. I have defined a audio file and a play
button.
<audio id="audio1" src="MDEud2F2"></audio>
<button onClick="play()">Play</button>
当我单击 play
按钮时,音频文件开始播放,但是当我再次单击该按钮时,音频文件不会停止并且不会再次播放,直到它到达文件末尾.
When I click the play
button the audio file starts playing, but when I click the button again the audio file doesn't stop and will not play again until it reaches the end of the file.
function play() {
document.getElementById('audio1').play();
}
有没有一种方法可以让我在使用 onclick
单击按钮时重新启动音频文件,而不是等待歌曲停止?
Is there a method that would allow me to restart the audio file when I click the button using onclick
rather than waiting for the song to stop?
推荐答案
要重新播放歌曲,您可以:
To just restart the song, you'd do:
function play() {
var audio = document.getElementById('audio1');
if (audio.paused) {
audio.play();
}else{
audio.currentTime = 0
}
}
FIDDLE
要切换它,就像再次单击时音频停止,而当再次单击时它会从头开始重新启动,您可以执行类似的操作:
To toggle it, as in the audio stops when clicking again, and when click another time it restarts from the beginning, you'd do something more like :
function play() {
var audio = document.getElementById('audio1');
if (audio.paused) {
audio.play();
}else{
audio.pause();
audio.currentTime = 0
}
}
FIDDLE
这篇关于播放音频并单击重新启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:播放音频并单击重新启动


- 400或500级别的HTTP响应 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Flexslider 箭头未正确显示 2022-01-01