0

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.

Community
  • 1
  • 1
Rootel
  • 149
  • 16
  • See this link: http://stackoverflow.com/questions/13402336/play-sound-file-in-a-web-page-in-the-background – Nach Sep 05 '16 at 20:56
  • 1
    FYI This is not possible with HTML5 audio properties. You need to implement this in Javascript (look for `onended` event). – Chris Sep 05 '16 at 21:14
  • http://stackoverflow.com/questions/35174154/how-to-play-multiple-mp3-songs-in-html – mlegg Sep 05 '16 at 22:21

1 Answers1

1

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!!!

SreYash
  • 111
  • 3
  • Hey, thanks. This helped in a way. I have one question. Where do I place the javascript code? I have my CSS and page details in the . Is that also where I place the javascript? Thank you! – Rootel Sep 06 '16 at 15:18
  • I tried to do it in head, it didn't work. It just counted that whole javascript as text. Do I need to download some kind of weird plugin for that? – Rootel Sep 06 '16 at 15:20
  • Try Adding javascript in – SreYash Sep 06 '16 at 16:03
  • Well, I finally had time to do it. This doesn't work. I putted it everywhere in my script (made a special empty html for it), doesn't work.. – Rootel Sep 15 '16 at 18:27
  • Add your `Script` tag just after `button` tag.I tried it in plane html file.It works fine. – SreYash Sep 18 '16 at 15:30
  • I already did that. I'm not getting a radio, im getting a weird list – Rootel Sep 19 '16 at 06:18