所以我用 JavaScript 编写了一个脚本来播放 mp3 文件,然后会出现一个图像,您可以单击该图像来切换是否应该播放音频。当我单击图片时,它会停止播放 mp3 文件,这正是我想要的,但是当我再次单击它时,它不会恢复或重新开始播放歌曲。
<script type="text/javascript">
var audio = document.getElementById("myAudio");
function detectmob() {
if( navigator.userAgent.match(/Android/i) //Checks if on mobile
|| navigator.userAgent.match(/webOS/i)
|| navigator.userAgent.match(/iPhone/i)
|| navigator.userAgent.match(/iPad/i)
|| navigator.userAgent.match(/iPod/i)
|| navigator.userAgent.match(/BlackBerry/i)
|| navigator.userAgent.match(/Windows Phone/i)
){
return true;
}
else {
audio.play();
}
}
function Pause() {
if (audio.play = false) {
audio.play()
}
else {
audio.pause();
}
}
window.onload = detectmob;
</script>
<center> <input type="image" onclick="Pause()" src="/images/Nor.gif" style="width:750px;height:400px;"></center>
最佳答案
您的条件检查audio.play = false
是错误的,它将值false
分配给play
属性覆盖默认函数引用。
您可以使用paused属性来检查音频是否暂停。
function Pause() {
if (audio.paused) {
audio.play();
} else {
audio.pause();
}
}
关于javascript - 打开和关闭音频按钮在 JS 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36733162/