用html做一个音乐播放器,有时长显示、上一首、下一首、暂停按钮,还有进度条,并且将它放在网页底部
时间: 2023-07-10 18:09:32 浏览: 171
html编写的音乐播放器,
以下是一个基本的 HTML 音乐播放器示例,包括时长显示、上一首、下一首、暂停按钮和进度条,并且将其放在网页底部:
```html
<!DOCTYPE html>
<html>
<head>
<title>HTML音乐播放器</title>
<style>
/* 音乐播放器样式 */
#music-player {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
background-color: #333;
color: #fff;
padding: 10px;
box-sizing: border-box;
}
#music-controls {
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 10px;
}
#music-controls button {
background-color: #666;
border: none;
color: #fff;
font-size: 16px;
padding: 10px;
margin: 0 5px;
cursor: pointer;
}
#music-time {
text-align: center;
font-size: 14px;
margin-bottom: 5px;
}
#music-progress {
background-color: #666;
height: 5px;
position: relative;
}
#music-progress-bar {
background-color: #fff;
height: 100%;
width: 0;
position: absolute;
top: 0;
left: 0;
}
</style>
</head>
<body>
<div id="music-player">
<audio id="music" src="your-music-file.mp3"></audio>
<div id="music-controls">
<button id="prev-btn">上一首</button>
<button id="play-btn">播放</button>
<button id="next-btn">下一首</button>
</div>
<div id="music-time">0:00 / 0:00</div>
<div id="music-progress">
<div id="music-progress-bar"></div>
</div>
</div>
<script>
// 获取元素
const music = document.getElementById('music');
const playBtn = document.getElementById('play-btn');
const prevBtn = document.getElementById('prev-btn');
const nextBtn = document.getElementById('next-btn');
const timeDisplay = document.getElementById('music-time');
const progressBar = document.getElementById('music-progress-bar');
// 设置音乐列表和当前歌曲索引
const playlist = ['song1.mp3', 'song2.mp3', 'song3.mp3'];
let currentTrack = 0;
// 更新时间显示和进度条
function updateTimeDisplay() {
const time = Math.floor(music.currentTime);
const duration = Math.floor(music.duration);
const minutes = Math.floor(time / 60);
const seconds = time % 60;
const totalMinutes = Math.floor(duration / 60);
const totalSeconds = duration % 60;
const timeString = `${minutes}:${seconds < 10 ? '0' : ''}${seconds} / ${totalMinutes}:${totalSeconds < 10 ? '0' : ''}${totalSeconds}`;
timeDisplay.innerText = timeString;
const progress = (time / duration) * 100;
progressBar.style.width = `${progress}%`;
}
// 播放/暂停音乐
function playPause() {
if (music.paused) {
music.play();
playBtn.innerText = '暂停';
} else {
music.pause();
playBtn.innerText = '播放';
}
}
// 上一首
function prevTrack() {
currentTrack--;
if (currentTrack < 0) {
currentTrack = playlist.length - 1;
}
music.src = playlist[currentTrack];
music.play();
playBtn.innerText = '暂停';
}
// 下一首
function nextTrack() {
currentTrack++;
if (currentTrack >= playlist.length) {
currentTrack = 0;
}
music.src = playlist[currentTrack];
music.play();
playBtn.innerText = '暂停';
}
// 给按钮添加事件监听器
playBtn.addEventListener('click', playPause);
prevBtn.addEventListener('click', prevTrack);
nextBtn.addEventListener('click', nextTrack);
// 监听音乐播放进度和结束事件
music.addEventListener('timeupdate', updateTimeDisplay);
music.addEventListener('ended', nextTrack);
</script>
</body>
</html>
```
你可以将 `your-music-file.mp3` 替换为你自己的音乐文件。如果你有多个音乐文件,可以将它们添加到 `playlist` 数组中,并使用上一首和下一首按钮切换歌曲。
阅读全文