<!DOCTYPE html>
<html>
<body>
<img id="myImage" onclick="aud_play_pause()" src="../../images/off.png" alt=""/>
<audio id="myAudio">
</audio>
<script>
function aud_play_pause()
{
var song = ["../Sesler/3.mp3"];
var myAudio = document.getElementById("myAudio");
var image = document.getElementById('myImage');
myAudio.src = song;
if (myAudio.paused)
{
image.src = "../../images/on.png";
myAudio.play();
}
else if(image.src.match("on"))
{
image.src = "../../images/off.png";
myAudio.pause();
}
}
</script>
</body>
</html>
您好,
我无法理解为什么它不起作用。我想点击(它开始)时开始,当我再次点击时它被认为是pouse(它没有)。我认为&#34;否则如果&#34;声明不起作用,但为什么?
谢谢,
小时。恰拉尔
答案 0 :(得分:0)
每次都设置音频src
,用户点击按钮。然后在加载后直接启动。将其移到函数外部并且不使用数组[...]
,但只使用字符串:
myAudio.src = "../Sesler/3.mp3";
然后,else
(没有其他条件)应足以区分播放和暂停:
if (myAudio.paused) {
image.src = "../../images/on.png";
myAudio.play();
} else {
image.src = "../../images/off.png";
myAudio.pause();
}