欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

html5 audio声音多次播放

程序员文章站 2022-06-13 08:22:16
...

 

 

<html>
<head>
	<title>js</title>
</head>
<body>

    <input id="bt" type="button" value="BT" >
    
	<script type="text/javascript">
	   var count = 2;
	   var music;
	
		var bt = document.getElementById("bt");     
		bt.addEventListener("click",function() {
			count = 2;
			a();
		});
		
		// html5:audio声音多次播放
		function a() {
			if (!music) {
				music = new Audio();
				music.addEventListener("ended", function (){
					if (count > 0) {
						a();
					}
				});
			} else {
				music.pause();
			}
			
			music.src = 'ding.mp3';
			music.play();
			
			console.log(count);
			count--;
		}
		
	</script>
	
</body>


</html>

 

 

 

 

 

 

相关标签: js audio