Add a "Previous" button to the player
[app-musicexpo.git] / player.js
CommitLineData
9b989f96
MG
1'use strict';
2var TYPES = {
3 "FLAC": "audio/x-flac",
4 "Vorbis": "audio/ogg",
5 "AAC": "audio/aac",
6 "MP3": "audio/mpeg"
7};
8
53fe8e9a 9var audio, details, start, prev, data;
447902f9 10var hash_to_id = {}, inhibit_handle_hash = false;
53fe8e9a 11var hist = []
9b989f96
MG
12
13function load_song (id) {
14 audio.style.display = "inline";
2e6185c6 15 var song = data[id];
9b989f96
MG
16 var old_sources = document.getElementsByTagName("source");
17 while(old_sources.length)
18 old_sources[0].parentNode.removeChild(old_sources[0]);
19
20 for (var i = 0 ; i < song.formats.length ; i++){
21 var source = document.createElement("source");
22 var type = TYPES[song.formats[i].format];
23 source.setAttribute("type", type);
2e6185c6 24 source.setAttribute("src", song.formats[i].file);
9b989f96
MG
25 audio.appendChild(source);
26 }
27
28 details.innerHTML = "Now playing: " + song.artist + " - " + song.title;
447902f9
MG
29 inhibit_handle_hash = true;
30 location.hash = song.hash;
31 inhibit_handle_hash = false;
9b989f96
MG
32 audio.load();
33}
34
35function play_random () {
36 start.innerHTML = "Next";
2e6185c6 37 var id = Math.floor(Math.random() * data.length);
53fe8e9a
MG
38 hist.push(id);
39 if(hist.length > 1)
40 prev.disabled = false;
9b989f96
MG
41 load_song(id);
42 audio.play();
43}
44
53fe8e9a
MG
45function play_random_after_error () {
46 hist.pop(); /* Purge offending song from history */
47 play_random();
48}
49
50function play_prev () {
51 hist.pop();
52 var song = hist[hist.length - 1];
53 console.log("Will now play: " + song)
54 if(hist.length <= 1)
55 prev.disabled = true;
56 load_song(song);
57 audio.play();
58}
59
60function handle_hash(first_run){
00b6ce9d 61 if(!hash_to_id.hasOwnProperty(location.hash) || inhibit_handle_hash)
447902f9 62 return;
53fe8e9a
MG
63 var id = hash_to_id[location.hash]
64 if(first_run === true)
65 hist.push(id)
66 load_song(id);
447902f9
MG
67 start.innerHTML = "Next";
68 audio.play();
2e6185c6
MG
69}
70
9b989f96
MG
71window.onload = function () {
72 var container = document.getElementById("player");
53fe8e9a 73 container.innerHTML = '<div id="details"></div> <button id="prev_player">Previous</button><button id="start_player">Play a random song</button> (or click a song title)<br><audio id="audio" controls></audio>';
9b989f96
MG
74 audio = document.getElementById("audio");
75 details = document.getElementById("details");
76 start = document.getElementById("start_player");
53fe8e9a
MG
77 prev = document.getElementById("prev_player")
78 prev.disabled = true;
9b989f96 79
2e6185c6
MG
80 data = [];
81 var trs = document.querySelectorAll("tbody tr");
82 for (var i = 0 ; i < trs.length ; i++) {
83 var tr = trs[i];
84 var song = {
85 "artist": tr.getElementsByClassName("artist")[0].textContent,
86 "title": tr.getElementsByClassName("title")[0].textContent,
447902f9 87 "hash": tr.getElementsByTagName("a")[0].dataset.hash,
2e6185c6
MG
88 "formats": []
89 };
90 var formats = tr.getElementsByClassName("formats")[0].getElementsByTagName("a");
91 for (var j = 0 ; j < formats.length ; j++) {
92 var format = formats[j];
93 song.formats.push({
94 "format": format.textContent,
95 "file": format.getAttribute("href")
96 });
97 }
98 data.push(song);
447902f9 99 hash_to_id[song.hash] = i;
2e6185c6 100 }
9b989f96
MG
101
102 audio.style.display = "none";
103 audio.addEventListener('ended', play_random);
53fe8e9a 104 audio.addEventListener('error', play_random_after_error);
9b989f96 105 start.addEventListener('click', play_random);
53fe8e9a 106 prev.addEventListener('click', play_prev);
447902f9 107 window.onhashchange = handle_hash;
53fe8e9a 108 handle_hash(true);
9b989f96 109};
This page took 0.016377 seconds and 4 git commands to generate.