Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 audio playlist - how to play a second audio file after the first has ended?

How could we make some audio in html5 play after another one has finished?

I have tried with jquery delay() function but it wont work at all, is it possible using pause() in html5 audio with timer instead ? For example, pause('500',function(){});?

like image 444
Khairu Aqsara Avatar asked Feb 17 '12 10:02

Khairu Aqsara


People also ask

How do I make the audio next to autoplay in HTML?

The HTML <audio> autoplay attribute is used to specify that the audio should automatically start playing as soon as it is loaded. It is a Boolean attribute.

Can multiple audio sources can be played in HTML?

The <audio> HTML element is used to embed sound content in documents. It may contain one or more audio sources, represented using the src attribute or the <source> element: the browser will choose the most suitable one.

What attribute can make the audio tag or video file to play again after it finished playing?

The loop attribute is a boolean attribute. When present, it specifies that the audio will start over again, every time it is finished.


2 Answers

If this is your audio tag:

<audio id="player" src="someAudio.mp3"/>

then adding an event listener to it for the "ended" event will make it possible to change the source and play your next sound.

var audio = document.getElementById("player");
audio.addEventListener("ended", function() {
    audio.src = "nextAudio.mp3";
    audio.play();
});
like image 170
Emil H Avatar answered Oct 22 '22 05:10

Emil H


Here's a JSLinted, unobtrusive Javascript example demonstrating how to handle and use the ended mediaevent. In your particular situation, you would trigger playback of the second audio file in your ended event handler.

You can use the code below or run the test fiddle.

Click an item in the playlist to begin playback. After one audio ends, the next will begin.

markup: (note the deliberate avoidance of whitespace between <li> elements - this is to simplify traversing the DOM with nextSibling.)

<audio id="player"></audio>

<ul id="playlist"><li data-ogg="http://www.lunerouge.org/sons/sf/LRWeird%201%20by%20Lionel%20Allorge.ogg">Space 1</li><li data-ogg="http://www.lunerouge.org/sons/sf/LRWeird%202%20by%20Lionel%20Allorge.ogg">Space 2</li><li data-ogg="http://www.lunerouge.org/sons/sf/LRWeird%203%20by%20Lionel%20Allorge.ogg">Space Lab</li></ul>

<button id="stop">Stop</button>

script:

// globals
var _player = document.getElementById("player"),
    _playlist = document.getElementById("playlist"),
    _stop = document.getElementById("stop");

// functions
function playlistItemClick(clickedElement) {
    var selected = _playlist.querySelector(".selected");
    if (selected) {
        selected.classList.remove("selected");
    }
    clickedElement.classList.add("selected");

    _player.src = clickedElement.getAttribute("data-ogg");
    _player.play();
}

function playNext() {
    var selected = _playlist.querySelector("li.selected");
    if (selected && selected.nextSibling) {
        playlistItemClick(selected.nextSibling);
    }
}

// event listeners
_stop.addEventListener("click", function () {
    _player.pause();
});
_player.addEventListener("ended", playNext);
_playlist.addEventListener("click", function (e) {
    if (e.target && e.target.nodeName === "LI") {
        playlistItemClick(e.target);
    }
});​


​
like image 25
Lloyd Avatar answered Oct 22 '22 04:10

Lloyd