Answer the question
In order to leave comments, you need to log in
How can I play an entire list of .mp3 files with an Audio() object?
Good day, dear connoisseurs!
There is a JavaScript code that should play all files one by one, by clicking on the button and pause when clicked back.
var tagAudio = document.getElementsByTagName("audio"); // получаем все ссылки
var array = []
for (let a of tagAudio) {
array.push(a.getAttribute('src')) // наполняем масив ссылками
}
var el = document.getElementById('player');
var playing = false; // текущее состояние плеера
var player = new Audio();
for (let i = 0; i < array.length; i++) {
player.src = array[i]
player.preload = "auto";
player.addEventListener('ended', function(){ // слушаем окончание трека
console.log("до", playing)
playing = false;
console.log("после", playing)
});
}
el.addEventListener('click', function(){ // слушаем клик по кнопке
player.preload = "auto";
if(playing){
player.pause();
} else {
player.play();
}
playing = !playing;
console.log("playing = !playing;", playing)
});
<button id="player" type="button">
Воспроизвести
</button>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question