gpt4 book ai didi

javascript - 如何播放选定的歌曲?

转载 作者:行者123 更新时间:2023-12-04 11:39:14 25 4
gpt4 key购买 nike

我有这个小 html 文本,我可以在其中选择一些我想听的歌曲。
怎么从这里我可以按下播放按钮,我选择的歌曲一个接一个地播放。换句话说,按顺序,第二个只有在第一个完成后才开始。
谢谢
'
新的 HTML 和脚本

<table id="tblsounds">
<tr>
<td><input id="chk1" type="checkbox" value="1" /><label for="chk01">Song nº 1</label></td>
</tr>
<td><input id="chk2" type="checkbox" value="2" /><label for="chk02">Song nº 2</label></td>
</tr>
<tr>
<td><input id="chk3" type="checkbox" value="3" /><label for="chk03">Song nº 3</label></td>
</tr>
<tr>
<td><input id="chk4" type="checkbox" value="4" /><label for="chk04">Song nº 4</label></td>
</tr>
<tr>
<td><input id="chk5" type="checkbox" value="5" /><label for="chk05">Song nº 5</label></td>
</tr>
<tr>
<td><input id="chk6" type="checkbox" value="6" /><label for="chk06">Song nº 6</label></td>
</tr>
<tr>
<td><input id="chk7" type="checkbox" value="7" /><label for="chk07">Song nº 7</label></td>
</tr>
<tr>
<td><input id="chk8" type="checkbox" value="8" /><label for="chk08">Song nº 8</label></td>
</tr>
<tr>
<td><input id="chk9" type="checkbox" value="9" /><label for="chk09">Song nº 9</label></td>
</tr>

<br><br>
</table>


<input type = "button" value = "Get" onclick = "GetSelected()" />

<button onClick="window.location.reload();">Remake your Playlist</button>

<script type="text/javascript">
function GetSelected() {
//Create an Array.
var selected = new Array();

//Reference the Table.
var tblsounds = document.getElementById("tblsounds");

//Reference all the CheckBoxes in Table.
var chks = tblsounds.getElementsByTagName("INPUT");

// Loop and push the checked CheckBox value in Array.
for (var i = 0; i < chks.length; i++) {
if (chks[i].checked) {
selected.push(chks[i].value);
}
}

//Display the selected CheckBox values.
if (selected.length > 0) {
alert("Selected values: " + selected.join(","));
}
};
</script>

Like this i get a array as result.

https://codepen.io/luis-pedro-the-sans/pen/rNmLYRp

最佳答案

有很多事情需要考虑。我为您准备了更新的代码,但请尝试了解那里发生了什么。让我们从 HTML 开始:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Playlist</title>
<script src="playlist.js" defer></script>
</head>
<body>
<table id="tblsounds">
<tr>
<td>
<label>
<input type="checkbox" value="sound/1.mp3">
Song nº 1
</label>
</td>
</tr>
<tr>
<td>
<label>
<input type="checkbox" value="sound/2.mp3">
Song nº 2
</label>
</td>
</tr>
<tr>
<td>
<label>
<input type="checkbox" value="sound/3.mp3">
Song nº 3
</label>
</td>
</tr>
<tr>
<td>
<label>
<input type="checkbox" value="sound/4.mp3">
Song nº 4
</label>
</td>
</tr>
<tr>
<td>
<label>
<input type="checkbox" value="sound/5.mp3">
Song nº 5
</label>
</td>
</tr>
<tr>
<td>
<label>
<input type="checkbox" value="sound/6.mp3">
Song nº 6
</label>
</td>
</tr>
<tr>
<td>
<label>
<input type="checkbox" value="sound/7.mp3">
Song nº 7
</label>
</td>
</tr>
<tr>
<td>
<label>
<input type="checkbox" value="sound/8.mp3">
Song nº 8
</label>
</td>
</tr>
<tr>
<td>
<label>
<input type="checkbox" value="sound/9.mp3">
Song nº 9
</label>
</td>
</tr>
</table>
<button id="clear" type="button">
Remake your Playlist
</button>
</body>
</html>
没什么太花哨的,我用他们的 <label>包裹了复选框克服使用 id 的需要.另外,我的文件路径为 value ,因为我们将使用它来跟踪当前正在播放的歌曲。只是其中一种方法。如您所见,我将音频保存在 sound/ 中。文件夹,因此如果您要将音频文件保存在其他位置,请更新值。
现在,更难的部分。这是 playlist.js 中更新的 JavaScript带有注释的文件:
'use strict';

const audio = new Audio();
const playlist = [];

document.getElementById('tblsounds').addEventListener('click', event => {
const isCheckbox = event.target.matches('input[type=checkbox]');

if (!isCheckbox) return;

const songFileName = event.target.value;

if (event.target.checked) {
// Adding the song to the playlist
playlist.push(songFileName);

if (playlist.length === 1) {
audio.src = songFileName;
audio.play();
}
} else {
// Removing the song from the playlist
const songIndex = playlist.indexOf(songFileName);

if (songIndex >= 0) {
const audioSrc = audio.getAttribute('src');

if (audioSrc === songFileName) {
audio.pause();

const nextSongIndex = songIndex + 1;

if (nextSongIndex < playlist.length) {
audio.src = playlist[nextSongIndex];
audio.play();
}
}

playlist.splice(songIndex, 1);
}
}
});

document.getElementById('clear').addEventListener('click', () => {
// Clear all checkboxes
document.getElementById('tblsounds').querySelectorAll('input[type=checkbox]').forEach(checkbox => {
checkbox.checked = false;
});

// Empty the playlist
playlist.splice(0, playlist.length);

audio.pause();
});

audio.addEventListener('ended', event => {
// Play the next song if available
const audioSrc = event.currentTarget.getAttribute('src');
const playlistIndex = playlist.indexOf(audioSrc);

if (playlistIndex >= 0) {
const nextSongIndex = playlistIndex + 1;

if (nextSongIndex < playlist.length) {
audio.src = playlist[nextSongIndex];
audio.play();
}
}
});
如您所见,我们不断更新 playlist 的内容与复选框交互时的数组。它会在当前歌曲完成后播放下一首歌曲,但前提是播放列表中有下一首歌曲。
我知道我们通常不会在 Stack Overflow 上发布完整的解决方案,但是由于您是初学者并且想要做一些非常复杂的事情(而且很有趣!),所以我决定帮助您。我希望你能从中学习:)

关于javascript - 如何播放选定的歌曲?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68298358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com