Rootel
Rootel

Reputation: 149

HTML5 <audio> Random Audio

i'm back from nowhere. I have something for school where we need to make a website. Everyone in my class uses those easy drag 'n drop builders. I'm ofcourse making it with Notepad++.

So, a 1990 looking page is ofcourse not enough with some fun music. I have found around 3 to 4 Mario Bros online music to use. But it only starts the first source link, never the others. I want to know how to do it, Google doesn't really help.

This is my code:

<audio autoplay="autoplay" controls="controls" title="Radio implented by Rootel"> 

So my question is, how do I autoplay this list? I didn't give a full list of the music, sorry.

Upvotes: 0

Views: 902

Answers (1)

SreYash
SreYash

Reputation: 111

Here you can make it with javascript!

//javascript
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);
    }
});
.selected {
    font-weight: bold;
    font-size:20px; 
}
<!--html-->
<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>

hope it helps!!!

Upvotes: 1

Related Questions