<!DOCTYPE html>
<html>
<head>
	<title>随机播放音乐</title>
	<script>
		// 创建一个音乐列表
		var musicList = [
			'music1.mp3',
			'music2.mp3',
			'music3.mp3',
			'music4.mp3',
			'music5.mp3'
		];
		// 随机选择一首音乐并播放
		function playRandomMusic(){
			var randomIndex = Math.floor(Math.random() * musicList.length);
			var audioPlayer = document.getElementById('audioPlayer');
			audioPlayer.src = musicList[randomIndex];
			audioPlayer.play();
		}
	</script>
</head>
<body>
	<h1>随机播放音乐</h1>
	<button onclick='playRandomMusic()'>播放随机音乐</button>
	<audio controls id='audioPlayer'>
		<source src='' type='audio/mpeg'>
	</audio>
</body>
</html>
HTML随机播放音乐代码示例

原文地址: https://www.cveoy.top/t/topic/lSmq 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录