[英]Javascript audio play on click and stop on click
Current code: 当前代码:
<script> function play() { var audio = document.getElementById("audio"); audio.play(); } </script> <button class="matrixBtn" value="PLAY" onclick="play()">star shopping</button> <audio id="audio" src="xxx"></audio>
When I press the button the music starts, which is exactly what I want. 当我按下按钮时,音乐开始播放,这正是我想要的。 But I also want if I press the button again that the music stops.
但是我也想如果我再次按下按钮,音乐就停止了。 How can I do this without create a second button?
如何在不创建第二个按钮的情况下执行此操作?
the <audio>
tag is a HTMLMediaElement
, so it has a property .paused
which you can use to determine the current state and act accordingly: <audio>
标签是HTMLMediaElement
,因此它具有.paused
属性,您可以使用该属性来确定当前状态并采取相应措施:
function playPause() {
var audio = document.getElementById("audio");
if(audio.paused)audio.play();
else audio.pause();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.