]> iEval git - app-musicexpo.git/commitdiff
Add a rudimentary player
authorMarius Gavrilescu <marius@ieval.ro>
Sun, 29 Mar 2015 09:27:36 +0000 (12:27 +0300)
committerMarius Gavrilescu <marius@ieval.ro>
Sun, 29 Mar 2015 09:27:50 +0000 (12:27 +0300)
MANIFEST
lib/App/MusicExpo.pm
player.js [new file with mode: 0644]

index f2ca44a2fd9dcaf0a508f0e11a2b5fd0949afc65..4bde63a9117696f6e99d0ec16cd3f2b8bc1f4ea7 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -11,4 +11,4 @@ empty.aac
 empty.mp3
 empty.flac
 empty.ogg
-
+player.js
index bf7707078223a04698aba63923e7d5e1b0b9a64a..96f9d8dda75d7c7680e0b99469bcdc2e2bcec456 100644 (file)
@@ -175,6 +175,9 @@ $default_template = <<'HTML';
 <title>Music</title>
 <meta charset="utf-8">
 <link rel="stylesheet" href="/music.css">
+<script async defer type="application/javascript" src="player.js"></script>
+
+<div id="player"></div>
 
 <table border>
 <thead>
diff --git a/player.js b/player.js
new file mode 100644 (file)
index 0000000..bccc451
--- /dev/null
+++ b/player.js
@@ -0,0 +1,49 @@
+'use strict';
+var TYPES = {
+       "FLAC": "audio/x-flac",
+       "Vorbis": "audio/ogg",
+       "AAC": "audio/aac",
+       "MP3": "audio/mpeg"
+};
+
+var audio, details, start, data;
+
+function load_song (id) {
+       audio.style.display = "inline";
+       var song = data.files[id];
+       var old_sources = document.getElementsByTagName("source");
+       while(old_sources.length)
+               old_sources[0].parentNode.removeChild(old_sources[0]);
+
+       for (var i = 0 ; i < song.formats.length ; i++){
+               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);
+               audio.appendChild(source);
+       }
+
+       details.innerHTML = "Now playing: " + song.artist + " - " + song.title;
+       audio.load();
+}
+
+function play_random () {
+       start.innerHTML = "Next";
+       var id = Math.floor(Math.random() * data.files.length);
+       load_song(id);
+       audio.play();
+}
+
+window.onload = function () {
+       var container = document.getElementById("player");
+       container.innerHTML = '<div id="details"></div> <button id="start_player">Start player</button><br><audio id="audio" controls></audio>';
+       audio = document.getElementById("audio");
+       details = document.getElementById("details");
+       start = document.getElementById("start_player");
+
+       data = JSON.parse(document.getElementById("json").innerHTML);
+
+       audio.style.display = "none";
+       audio.addEventListener('ended', play_random);
+       start.addEventListener('click', play_random);
+};
This page took 0.03142 seconds and 4 git commands to generate.