X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=player.js;h=7adcdd838d4cf4b58754f0174a8f0ca8a2e7f0e8;hb=refs%2Fheads%2Fmaster;hp=bccc451f30b9559a63300dd68de7f46007425ca8;hpb=9b989f9631e26e943f09be01e8846771112c695c;p=app-musicexpo.git diff --git a/player.js b/player.js index bccc451..7adcdd8 100644 --- a/player.js +++ b/player.js @@ -6,11 +6,13 @@ var TYPES = { "MP3": "audio/mpeg" }; -var audio, details, start, data; +var audio, details, start, prev, data; +var hash_to_id = {}, inhibit_handle_hash = false; +var hist = [] function load_song (id) { audio.style.display = "inline"; - var song = data.files[id]; + var song = data[id]; var old_sources = document.getElementsByTagName("source"); while(old_sources.length) old_sources[0].parentNode.removeChild(old_sources[0]); @@ -19,31 +21,89 @@ function load_song (id) { var source = document.createElement("source"); var type = TYPES[song.formats[i].format]; source.setAttribute("type", type); - source.setAttribute("src", data.prefix + song.formats[i].file); + source.setAttribute("src", song.formats[i].file); audio.appendChild(source); } details.innerHTML = "Now playing: " + song.artist + " - " + song.title; + inhibit_handle_hash = true; + location.hash = song.hash; + inhibit_handle_hash = false; audio.load(); } function play_random () { start.innerHTML = "Next"; - var id = Math.floor(Math.random() * data.files.length); + var id = Math.floor(Math.random() * data.length); + hist.push(id); + if(hist.length > 1) + prev.disabled = false; load_song(id); audio.play(); } +function play_random_after_error () { + hist.pop(); /* Purge offending song from history */ + play_random(); +} + +function play_prev () { + hist.pop(); + var song = hist[hist.length - 1]; + console.log("Will now play: " + song) + if(hist.length <= 1) + prev.disabled = true; + load_song(song); + audio.play(); +} + +function handle_hash(first_run){ + if(!hash_to_id.hasOwnProperty(location.hash) || inhibit_handle_hash) + return; + var id = hash_to_id[location.hash] + if(first_run === true) + hist.push(id) + load_song(id); + start.innerHTML = "Next"; + audio.play(); +} + window.onload = function () { var container = document.getElementById("player"); - container.innerHTML = '