本帖最后由 20230713G001133 于 2024-3-4 13:25 编辑
添加一个CSS
<!DOCTYPE html>
<html>
<head>
<title>音乐播放器</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: rgb(255, 0, 128); /* 设置背景色为RGB(255,0,128)*/
color: white; /* 设置文字颜色为白色 */
}
h1 {
margin-top: 20px;
}
button {
padding: 10px 20px;
margin: 10px;
font-size: 16px;
cursor: pointer;
}
ul {
list-style: none;
padding: 0;
}
li {
margin: 5px;
cursor: pointer;
}
audio {
margin-top: 20px;
}
#playlist {
display: none;
}
</style>
<script>
var songs = [
// 更多歌曲...
];
var currentSongIndex = 0;
var isRandom = false;
var playlistVisible = false; // 新增变量,用于跟踪播放列表的显示状态
window.onload = function() {
var audio = document.getElementById("audioPlayer");
audio.addEventListener('ended', playNextSong);
playSong(currentSongIndex);
};
function playRandomSong() {
var randomIndex = Math.floor(Math.random() * songs.length);
playSong(randomIndex);
}
function playSong(index) {
var audio = document.getElementById("audioPlayer");
audio.src = songs[index];
audio.play();
currentSongIndex = index;
var currentSongUrl = document.getElementById("currentSongUrl");
currentSongUrl.textContent = "正在播放:" + songs[index];
}
function playNextSong() {
if(isRandom) {
playRandomSong();
} else {
currentSongIndex = (currentSongIndex + 1) % songs.length;
playSong(currentSongIndex);
}
}
function playPreviousSong() {
if(isRandom) {
playRandomSong();
} else {
currentSongIndex = (currentSongIndex - 1 + songs.length) % songs.length;
playSong(currentSongIndex);
}
}
function togglePlayMode() {
isRandom = !isRandom;
document.getElementById("playMode").textContent = isRandom ? "随机播放" : "顺序播放";
}
function togglePlaylist() {
playlistVisible = !playlistVisible;
var playlistContainer = document.getElementById("playlist");
if (playlistVisible) {
showPlaylist();
playlistContainer.style.display = "block";
} else {
playlistContainer.style.display = "none";
}
}
function showPlaylist() {
var playlistContainer = document.getElementById("playlist");
playlistContainer.innerHTML = ""; // 清空当前列表
songs.forEach(function(song, index) {
var songElement = document.createElement("li");
songElement.textContent = song;
songElement.onclick = function() { playSong(index); }; // 添加点击事件
playlistContainer.appendChild(songElement);
});
}
</script>
</head>
<body>
<h1 id="currentSongUrl">请选择歌曲播放</h1>
<audio id="audioPlayer" controls></audio>
<br/>
<button>上一首</button>
<button id="playMode">顺序播放</button>
<button>下一首</button>
<button>显示/隐藏播放列表</button>
<ul id="playlist" style="display:none;"></ul> <!-- 播放列表容器 -->
</body>
</html>
|