Skip to content

Instantly share code, notes, and snippets.

@unyo
Created May 11, 2016 21:22
Show Gist options
  • Save unyo/b1cc0d7a0ae6020766190b05ef00f00b to your computer and use it in GitHub Desktop.
Save unyo/b1cc0d7a0ae6020766190b05ef00f00b to your computer and use it in GitHub Desktop.
MP3 Player for simple http server with file listing
<script src="https://code.jquery.com/jquery-2.2.3.js" integrity="sha256-laXWtGydpwqJ8JA+X9x2miwmaiKhn8tVmOVEigRNtP4=" crossorigin="anonymous"></script>
<script>
$(function() {
var audio;
var playlist;
var tracks;
var current;
fetch('./').then((res) => {
res.text().then((text) => {
let tmp = $('<div />').html(text)
let links = tmp.find("a[href$='.mp3']")
$('#playlist').html(links)
init()
return tmp
})
})
function init(){
current = 0;
audio = $('audio');
playlist = $('#playlist');
tracks = playlist.find('a');
len = tracks.length - 1;
audio[0].play();
playlist.find('a').click(function(e) {
e.preventDefault();
link = $(this);
current = link.index();
run(link, audio[0]);
});
audio[0].addEventListener('ended', function(e){
current++;
if(current == len){
current = 0;
link = playlist.find('a')[0];
}else{
link = playlist.find('a')[current];
}
run($(link),audio[0]);
});
}
function run(link, player){
player.src = link.attr('href');
audio[0].load();
audio[0].play();
}
})
</script>
<style>
#playlist a { display: block }
</style>
<audio id="audio" preload="auto" tabindex="0" controls="" type="audio/mpeg">
</audio>
<div id="playlist">
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment