<embed src="..."> 只能播放單首,如何添加多首並實現切換按鈕?
<embed id="player" src="song1.mp3" autostart="true" loop="true" hidden="false" />
<button onclick="play('song2.mp3')">切換到歌2</button>
<button onclick="play('song3.mp3')">切換到歌3</button>
<script>
function play(url) {
document.getElementById('player').src = url;
}
</script>
const songs = ['song1.mp3', 'song2.mp3', 'song3.mp3'];
let idx = 0;
const player = document.getElementById('player');
function play(idx) {
player.src = songs[idx];
}
function next() { play((idx + 1) % songs.length); }
function prev() { play((idx - 1 + songs.length) % songs.length); }
<audio id="player" controls></audio>
<script>
const player = document.getElementById('player');
player.addEventListener('ended', () => next()); // 自動下一首
// 其餘邏輯同前
</script>
ended 事件實現自動播放列表。playbackRate, volume, currentTime 等。<embed> 是上世紀產物,新項目請直接用 <audio>。