Repository navigation
Expand file tree
/
Copy pathmusic.js
More file actions
73 lines (65 loc) · 1.67 KB
/
Copy pathmusic.js
File metadata and controls
73 lines (65 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var songs = [
{
file: 'audio/jonobacon-freesoftwaresong2.ogg',
title: 'The Free Software Song',
artist: 'Jono Bacon'
},
{
file: 'audio/b2g.ogg',
title: 'Boot to Gecko',
artist: 'Brendan Eich'
},
{
file: 'audio/Salt_Creek.ogg',
title: 'Salt Creek',
artist: 'The Rogue Bluegrass Band'
},
{
file: 'audio/treasure_island_01-02_stevenson.ogg',
title: 'Treasure Island',
artist: 'Read by Adrian Praetzellis'
}
];
window.addEventListener('DOMContentLoaded', function() {
var songList = document.getElementById('songs');
var player = document.getElementById('player');
var audio = document.getElementById('playerAudio');
var content = '';
songs.forEach(function(song) {
content += '<li class="song">' +
' <a id="' + song.file + '" href="#">' +
' ' + song.title + ' - ' + song.artist +
' </a>' +
'</li>';
});
songList.innerHTML = content;
songList.addEventListener('click', function(evt) {
var target = evt.target;
if (!target)
return;
playSong(target.id); // song url is anchor id
});
window.addEventListener('keyup', function keyPressHandler(evt) {
if (playing && evt.keyCode == evt.DOM_VK_ESCAPE) {
stopSong();
showSongList();
evt.preventDefault();
}
});
var playing = false;
function showSongList(songs) {
songList.classList.remove('hidden');
player.classList.add('hidden');
playing = false;
}
function playSong(url) {
songList.classList.add('hidden');
player.classList.remove('hidden');
audio.src = url;
playing = true;
}
function stopSong(song) {
audio.pause();
playing = false;
}
});