I have a javascript playlist function that works great with the online audio playlist.
If the list with the tag a
is HTTPS:// type address, it works fine.
This is the original that works fine (with URLs on the web).
But when the local href
tag a
is of type ../list/audio.mp3 , (or similar) the next track doesn't play. Plays only when selected with click
.
I'm not sure why the playlist with a local address in href
the playlist in HTML5 doesn't work
init();
function init() {
var audio = document.getElementById("audio");
var playlist = document.getElementById('playlist');
var tracks = playlist.getElementsByTagName('a');
audio.volume = 0.10;
audio.play();
for(var track in tracks) {
var link = tracks[track];
if(typeof link === "function" || typeof link === "number") continue;
link.addEventListener("click", function(e) {
e.preventDefault();
var song = this.getAttribute('href');
run(song, audio, this);
});
}
audio.addEventListener("ended", function(e) {
for(var track in tracks) {
var link = tracks[track];
var nextTrack = parseInt(track) + 1;
if(typeof link === "function" || typeof link === "number") continue;
if(!this.src) this.src = tracks[0];
if(track == (tracks.length - 1)) nextTrack = 0;
console.log(nextTrack);
if(link.getAttribute("href") === this.src) {
var nextLink = tracks[nextTrack];
run(nextLink.getAttribute('href'), audio, nextLink);
break;
}
}
});
}
function run(song, audio, link) {
var parent = link.parentElement;
//quitar el active de todos los elementos de la lista
var items = parent.parentElement.getElementsByTagName('li');
for(var item in items) {
if(items[item].classList) items[item].classList.remove("active");
}
//agregar active a este elemento
parent.classList.add("active");
//tocar la cancion
audio.src = song;
audio.load();
audio.play();
}
audio {
display: block;
width: 100%;
max-width: 300px;
}
#playlist {
display: block;
width: 100%;
max-width: 300px;
padding: 10px 5px;
list-style: none;
}
a {
text-decoration: none;
}
.active a {
color: #5DB0E6;
text-decoration: none;
}
li a {
color: #eeeedd;
background: #333;
padding: 5px;
display: block;
}
<audio id="audio" preload="auto" tabindex="0" controls="" >
<source src="../audio/uno.mp3">
</audio>
<ul id="playlist">
<li class="active">
<a href="../audio/uno.mp3">Audio uno</a>
</li>
<li>
<a href="../audio/dos.mp3">Audio dos</a>
</li>
</ul>
There are several things to correct in your code:
var
, because they are declared in global scope and can interfere with other parts of your code. I recommend you read this article to know more about it.nextTrack
and exit the loop withbreak
, but the .findIndex() method makes that easy.forEach()
, although you should keep in mind that this is more a matter of preference than optimizationWhat if the index of the song being played is not found?
Sometimes the browser will add protocol and domain to properties
href
,src
and the like, causing comparisons to fail. If so, you look up the index of the list item that has the active class and use it to advance playback.Change this line:
For these two:
The audio link is in the same position as the active element.