This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudioPlayer.html
160 lines (145 loc) · 5.03 KB
/
audioPlayer.html
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<!doctype html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>Audio Player</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="html5 audio player">
<meta name="author" content="EnergeticPixelsLLC">
<link rel="stylesheet" href="player.css">
<script src='music.js'></script>
<!--[if lt IE 9]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<audio id='player'>
<!-- OLD Source *****
<source src='sample.mp3' />
<source src='sample.ogg' />
-->
</audio>
<button id='btnPlay'>Play</button>
<button id='btnPause'>Pause</button>
<button id='btnStop'>Stop</button>
<br />
<button id='btnVolUp'>Volume +</button>
<button id='btnVolDn'>Volume -</button>
<br />
<span id='songTime'></span>
<br />
<input id='sliderTime' type='range' min='0' value='0' />
<div id='musicList'></div>
<script>
var player;
var intv;
var slider;
// INIT *************************
window.onload = function() {
document.getElementById('btnPlay').addEventListener('click', playMusic, false);
document.getElementById('btnPause').addEventListener('click', pauseMusic, false);
document.getElementById('btnStop').addEventListener('click', stopMusic, false);
document.getElementById('btnVolUp').addEventListener('click', volumeUp, false);
document.getElementById('btnVolDn').addEventListener('click', volumeDn, false);
player = document.getElementById('player');
slider = document.getElementById('sliderTime');
slider.addEventListener('change', reposition, false);
getMusicList();
};
// moving playhead to specific time by moving slider
function reposition() {
player.currentTime = slider.value;
};
// update interval thing
function update() {
document.getElementById('songTime').innerHTML = millisToMins(player.currentTime);
slider.value = player.currentTime;
};
function millisToMins(seconds) {
// calculating number of minutes and seconds in each
var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);
var numseconds = (((seconds % 31536000) % 86400) % 3600) % 60;
// getting time code to display correctly. less than 10 seconds
// will get a 0 prepended to the time code
// Math.round is used to get just a two digit seconds display
if(numseconds >= 10) {
return "Time elapsed: " + numminutes + ":" + Math.round(numseconds);
} else {
return "Time elapsed: " + numminutes + ":0" + Math.round(numseconds);
}
};
// music play controls **************
function playMusic() {
player.play();
intv = setInterval(update, 100);
slider.max = player.duration;
};
// music pause controls ******
function pauseMusic() {
player.pause();
// without clearInterval the update intervals will keep running
clearInterval(intv);
};
// music stop controls *********
function stopMusic() {
// there is no 'stop' method in html5 audio element
// so we have to trick it by pausing the playhead and then
// resetting the playhead to the beginning of audio time
player.pause();
player.currentTime = 0;
// without clearInterval the update intervals will keep running
clearInterval(intv);
};
// increase the volume
// html5 volume ranges from 0.0 to 1.0
function volumeUp() {
if(player.volume <= 1) {
// increasing 10% for every click of button
player.volume += 0.1;
console.log(player.volume);
} else {
player.volume = 1;
}
};
// turn the volume down
function volumeDn() {
if(player.volume >= 0) {
player.volume -= 0.1;
console.log(player.volume);
} else {
player.volume = 0;
}
};
function getMusicList() {
var parser = new DOMParser();
xmlDocument = parser.parseFromString(xml, "text/xml");
//console.log(xmlDocument);
var elementsArray = xmlDocument.documentElement.getElementsByTagName('composition');
//console.log(elementsArray);
var arrayLength = elementsArray.length;
var output = "<table>";
for(var i = 0; i < arrayLength; i++) {
var title = elementsArray[i].getElementsByTagName('title')[0].firstChild.nodeValue;
//console.log(title);
var composer = elementsArray[i].getElementsByTagName('title')[0].firstChild.nodeValue;
//console.log(title);
var composer = elementsArray[i].getElementsByTagName('composer')[0].firstChild.nodeValue;
var time = elementsArray[i].getElementsByTagName('time')[0].firstChild.nodeValue;
var fileName = elementsArray[i].getElementsByTagName('filename')[0].firstChild.nodeValue;
//console.log(title + " " + composer + " " + time + " " + fileName);
output += "<tr>";
output += ("<td onclick='songSelect(\"" + fileName + "\")'>" + title + " By: " + composer + "</td>");
output += "</tr>";
}
output += "</table>";
document.getElementById('musicList').innerHTML = output;
};
function songSelect(fn) {
//console.log(fn);
var song = document.getElementById('player').src = fn;
console.log(song);
playMusic();
};
</script>
</body>
</html>