-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.js
148 lines (121 loc) · 4.88 KB
/
player.js
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
(function(d) {
var clips;
createClipHTML();
processClips();
function createClipHTML() {
var clipData = {
"clips": [{
"title": "Clip 1",
"artist": "Artist 1",
"media": {
"src": "SoundClips/clip1.mp3",
"type": "audio/mpeg"
}
}, {
"title": "Clip 2",
"artist": "Artist 2",
"media": {
"src": "SoundClips/clip2.mp3",
"type": "audio/mpeg"
}
}]
}
var clipTemplate = '<div class="clipContainer"> \
<div id="#id" class="play"></div> \
<p class="artist">#artist: <span class="title">#title</span></p> \
<div id="#resetid" class="reset"></div> \
<div id="#progressid" class="progress"> \
<div id="#progressbarid" class="progressbar"></div> \
</div> \
<audio id="#clipid"> \
<source src="#clipSource" type="#clipType"></source> \
</audio> \
</div>'
var clipHTML = "";
for (var i = 0; i < clipData.clips.length; i++) {
var newClip = clipTemplate
.replace("#id", i + 1)
.replace("#resetid", "reset" + (i + 1))
.replace("#progressid", "progress" + (i + 1))
.replace("#progressbarid", "progressbar" + (i + 1))
.replace("#clipid", "clip" + (i + 1))
.replace("#artist", clipData.clips[i].artist)
.replace("#title", clipData.clips[i].title)
.replace("#clipSource", clipData.clips[i].media.src)
.replace("#clipType", clipData.clips[i].media.type);
clipHTML += newClip
};
d.body.innerHTML = clipHTML;
}
function processClips() {
clips = d.getElementsByTagName("audio");
clips.activeClip = "";
for (var i = clips.length - 1; i >= 0; i--) {
processProgressBar(clips[i]);
processResetButton(clips[i]);
processPlayPauseDiv(clips[i]);
processClip(clips[i]);
}
}
function processProgressBar(clip) {
var progress = d.getElementById("progress" + (clip.id[clip.id.length-1]));
progress.rect = progress.getBoundingClientRect();
progress.clip = clip;
clip.progress = progress;
progress.onclick = function(xy) {
processClickTouch(xy, this);
}
progress.ontouchstart = function(xy) {
processClickTouch(xy, this);
}
function processClickTouch(mouseEvent, progressBar) {
progressBar.clip.currentTime =
progressBar.clip.duration *
((mouseEvent.clientX -
progressBar.rect.left) /
(progressBar.rect.right - progressBar.rect.left));
}
}
function processResetButton(clip) {
var resetButton = d.getElementById("reset" + (clip.id[clip.id.length-1]));
resetButton.clip = clip;
resetButton.onclick = function(mouseEvent) {
this.clip.currentTime = 0;
}
}
function processPlayPauseDiv(clip) {
var playPauseDiv = d.getElementById((clip.id[clip.id.length-1]));
playPauseDiv.clip = clip;
clip.playPauseDiv = playPauseDiv;
playPauseDiv.onclick = function(mouseEvent) {
if (this.className == "pause") {
this.clip.pause();
} else {
if (typeof(clips.activeClip.id) != "undefined") {
clips.activeClip.pause();
}
this.clip.play();
}
}
}
function processClip(clip) {
clip.ontimeupdate = function() {
var id = this.id;
var progress = (this.currentTime / this.duration) * 100;
this.progress.childNodes[1].style.width = progress + '%';
if (this.currentTime == this.duration) {
this.currentTime = 0;
this.progress.childNodes[1].style.width = "0%";
this.playPauseDiv.className = "play";
}
}
clip.onpause = function() {
clips.activeClip = "";
this.playPauseDiv.className = "play";
}
clip.onplay = function() {
clips.activeClip = this;
this.playPauseDiv.className = "pause";
}
}
})(document);