[英]How to stop previous song after clicking next images by using JavaScript
我正在尝试制作一个简单的游戏,就像那里有很多动物图像一样。 我想在点击图像后播放一首歌。 我已经做到了。 但问题是点击下一张图片后,第一张图片歌曲应该停止,然后下一张图片歌曲将继续播放。 这是示例 cide
<!DOCTYPE html>
<html>
<head>
<title>Anmal Sounds</title>
</head>
<body>
<script type="text/javascript">
function myAudioFunction(letter) {
var audio = new Audio('C:\\Users\\SONU\\Desktop\\animal\\'+letter+'.mp3');
audio.play();
}
</script>
<a onclick="myAudioFunction('tiger');">
<img src="C:\Users\SONU\Desktop\animal\tiger.jpeg" height="110px" width="110px">
</a>
<a onclick="myAudioFunction('cat');">
<img src="C:\Users\SONU\Desktop\animal\cat.jpg" height="110px" width="110px">
</a>
<a onclick="myAudioFunction('elephant');">
<img src="C:\Users\SONU\Desktop\animal\elephant.jpg" height="110px" width="110px">
</a>
<a onclick="myAudioFunction('deer');">
<img src="C:\Users\SONU\Desktop\animal\deer.jpg" height="110px" width="110px">
</a>
</body>
</html>
when I click next images the 1st I ages song still continued. But how to stop previous one and play next one
任何人都有任何想法
How to solve this problem.
在重新分配audio
变量之前,您是否尝试过audio.pause()
?
由于尚未声明音频,此命令在第一次执行时可能会返回错误,但您可以通过执行if(audio) audio.pause()
来解决此问题。
您需要将音频 object 放在外面的简单解决方案,如下所示。
<script type="text/javascript">
var audio = new Audio();
function myAudioFunction(letter) {
audio.pause()
audio = new Audio('C:\\Users\\SONU\\Desktop\\animal\\'+letter+'.mp3');
audio.play();
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.