Я пытаюсь улучшить свои знания JS, создав простого музыкального проигрывателя в HTML, JS и Tailwind CSS. < /p>
Я пытаюсь добавить кнопки DOM ко всем аудио. Я ожидаю, что кнопки воспроизводят аудио, если вы нажмете на него. Хотя он не воспроизводит звук и не показывает никаких ошибок. Я не понимаю, что не так с моим кодом. < /P>
const allSongs = [{
id: 0,
title: "Scratching The Surface",
artist: "Quincy Larson",
duration: "4:25",
src: //here is a link
},
{
id: 1,
title: "Can't Stay Down",
artist: "Quincy Larson",
duration: "4:15",
src: //here is a link
},
{
id: 2,
title: "Still Learning",
artist: "Quincy Larson",
duration: "3:51",
src: //here is a link
},
{
id: 3,
title: "Cruising for a Musing",
artist: "Quincy Larson",
duration: "3:34",
src: //here is a link
},
{
id: 4,
title: "Never Not Favored",
artist: "Quincy Larson",
duration: "3:35",
src: //here is a link
},
{
id: 5,
title: "From the Ground Up",
artist: "Quincy Larson",
duration: "3:12",
src: //here is a link
},
{
id: 6,
title: "Walking on Air",
artist: "Quincy Larson",
duration: "3:25",
src: //here is a link
},
{
id: 7,
title: "Can't Stop Me. Can't Even Slow Me Down.",
artist: "Quincy Larson",
duration: "3:52",
src: //here is a link
},
{
id: 8,
title: "The Surest Way Out is Through",
artist: "Quincy Larson",
duration: "3:10",
src: //here is a link
},
{
id: 9,
title: "Chasing That Feeling",
artist: "Quincy Larson",
duration: "2:43",
src: //here is a link
},
];
const songs = allSongs.sort((a, b) => a.title.localeCompare(b.title));
const currentSongTime = 0;
const currentSong = null;
const song = songs.find((song, id) => song.id === id);
const audio = new Audio();
const renderSongs = () => {
songs.forEach(data => {
playlist.innerHTML += `
[*]
${data.title}
${data.artist}
${data.duration}
`;
});
playlist.appendChild(li);
playlist.appendChild(playlistBtn);
playlist.appendChild(removeBtn);
// by clicking on a song, it will play a clicked song
playlistBtn.addEventListener("click", () => {
audio.src = song.src;
audio.play();
})
}
renderSongs();
Я пытаюсь улучшить свои знания JS, создав простого музыкального проигрывателя в HTML, JS и Tailwind CSS. < /p> Я пытаюсь добавить кнопки DOM ко всем аудио. Я ожидаю, что кнопки воспроизводят аудио, если вы нажмете на него. Хотя он не воспроизводит звук и не показывает никаких ошибок. Я не понимаю, что не так с моим кодом. < /P>
[code]const allSongs = [{ id: 0, title: "Scratching The Surface", artist: "Quincy Larson", duration: "4:25", src: //here is a link }, { id: 1, title: "Can't Stay Down", artist: "Quincy Larson", duration: "4:15", src: //here is a link }, { id: 2, title: "Still Learning", artist: "Quincy Larson", duration: "3:51", src: //here is a link }, { id: 3, title: "Cruising for a Musing", artist: "Quincy Larson", duration: "3:34", src: //here is a link }, { id: 4, title: "Never Not Favored", artist: "Quincy Larson", duration: "3:35", src: //here is a link }, { id: 5, title: "From the Ground Up", artist: "Quincy Larson", duration: "3:12", src: //here is a link }, { id: 6, title: "Walking on Air", artist: "Quincy Larson", duration: "3:25", src: //here is a link }, { id: 7, title: "Can't Stop Me. Can't Even Slow Me Down.", artist: "Quincy Larson", duration: "3:52", src: //here is a link }, { id: 8, title: "The Surest Way Out is Through", artist: "Quincy Larson", duration: "3:10", src: //here is a link }, { id: 9, title: "Chasing That Feeling", artist: "Quincy Larson", duration: "2:43", src: //here is a link }, ];
const songs = allSongs.sort((a, b) => a.title.localeCompare(b.title)); const currentSongTime = 0; const currentSong = null; const song = songs.find((song, id) => song.id === id); const audio = new Audio();
// by clicking on a song, it will play a clicked song playlistBtn.addEventListener("click", () => { audio.src = song.src; audio.play(); }) } renderSongs();[/code]