diff options
| author | Jules Laplace <jules@okfoc.us> | 2015-09-07 08:03:08 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2015-09-07 08:03:08 -0400 |
| commit | 5f8dcf6f299b37f2c46fdbd9b047017fb7dea8fd (patch) | |
| tree | 8dcd47aa5f010a103bc21e61d151046da4bd5077 /public/assets/js/lib/views/details/audio.js | |
| parent | 694dbdf6ff3eef3e33fcbe95ebd676eea3353dcc (diff) | |
audio player
Diffstat (limited to 'public/assets/js/lib/views/details/audio.js')
| -rw-r--r-- | public/assets/js/lib/views/details/audio.js | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/public/assets/js/lib/views/details/audio.js b/public/assets/js/lib/views/details/audio.js new file mode 100644 index 0000000..6a8f5ed --- /dev/null +++ b/public/assets/js/lib/views/details/audio.js @@ -0,0 +1,69 @@ +var audio = (function(){ + var audio = {} + + var el, music = [], current_index = -1 + var links, comment, parent + + audio.init = function () { + links = document.querySelectorAll("a") + comment = document.querySelector("#comment") + parent = document.querySelector("#audio") + + Array.prototype.slice.apply(links).forEach(function(url){ + if (! url.href.match(/(mp3|wav|ogg)/)) return + url.dataset.index = music.length + music.push(url) + }) + audio.build() + } + audio.build = function () { + el = document.createElement("audio") + el.setAttribute("controls", true) + el.addEventListener("ended", audio.next) + el.src = music[0] + parent.appendChild(el) + document.body.addEventListener("keydown", audio.keydown) + } + audio.destroy = function () { + el.pause() + el = null + parent.removeChild(el) + document.body.removeEventListener("keydown", audio.keydown) + } + audio.play = function (index) { + current_index = (parseInt(index) + music.length) % music.length + el.src = music[current_index].href + el.play() + var playing = document.querySelector(".playing") + if (playing) playing.classList.remove("playing") + music[current_index].classList.add("playing") + } + audio.prev = function (){ + audio.play(current_index - 1) + } + audio.next = function (){ + audio.play(current_index + 1) + } + audio.toggle = function (){ + if (el.paused) el.play() + else el.pause() + } + audio.keydown = function(e){ + if (app.typing || e.ctrlKey || e.altKey || e.metaKey || e.shiftKey) return + switch (e.keyCode) { + case 37: // left + audio.prev() + break; + case 39: // right + audio.next() + break; + case 32: // spacebar + e.preventDefault() + audio.toggle() + break; + } + } + + return audio + +})()
\ No newline at end of file |
