-
Notifications
You must be signed in to change notification settings - Fork 0
/
DEMO_h5-audio.html
351 lines (326 loc) · 9.53 KB
/
DEMO_h5-audio.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="font-awesome/css/font-awesome.min.css">
<title>my audio</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
padding: 5%;
font-family: "微软雅黑";
-webkit-tap-highlight-color: transparent;
}
.audio-container {
width: 438px;
background: yellow;
}
.my-audio {
font-size: 0;
margin: 0 auto;
position: relative;
background: rgba(60, 60, 75, 0.9);
width: 100%;
height: 44px;
}
.button-box {
display: inline-block;
width: 44px;
text-align: center;
height: 100%;
}
.play-controller {
position: relative;
top: 50%;
transform: translate(0,-50%);
border: transparent;
color: #fff;
font-size: 22px!important;
cursor: pointer;
}
.bar-wrapper {
vertical-align: top;
display: inline-block;
width: 320px;
height: 100%;
}
.progress-bar {
position: relative;
top: 50%;
transform: translate(0,-50%);
width: 100%;
height: 6px;
background: rgba(157, 158, 158, 1);
font-size: 0;
}
.buffered-range {
display: inline-block;
background: rgba(60, 60, 75, 1);
height: inherit;
}
.played-range {
display: inline-block;
width: 0;
background: rgba(216, 216, 219, 1);
height: inherit;
}
.drag-ball {
position: relative;
height: 12px;
width: 12px;
left: 100%;
top:50%;
transform: translate(-50%,-50%);
border-radius: 50px;
background: #fff;
}
.time-box {
float: right;
margin-top: 30px;
width: 100px;
font-size: 10px;
color: #fff;
}
.time-detail {
position: relative;
text-align: center;
top: 50%;
transform: translate(0,-50%);
}
.speed-box {
float: right;
clear: both;
width: 44px;
height: 100%;
color: #fff;
}
.speed {
position: relative;
top: 50%;
transform: translate(0,-50%);
}
.speed p {
text-align: center;
font-size: 10px;
}
</style>
<script type="text/javascript" src="EventEmitter.js"></script>
<script type="text/javascript" src="jquery.min.js"></script>
</head>
<body>
<audio id="audio" src="" loop="loop"></audio>
<div class="audio-container">
<div id="my-audio" class="my-audio">
<div class="button-box">
<i id="play-controller" class="fa fa-play play-controller"></i>
</div>
<div id="bar-wrapper" class="bar-wrapper">
<div id="progress-bar" class="progress-bar">
<div id="played-range" class="played-range">
<div id="drag-ball" class="drag-ball"></div>
</div>
<div id="buffered-range" class="buffered-range"></div>
</div>
<div class="time-box">
<div class="time-detail">
<span id="current-time">00:00:00</span>/<span id="audio-duration">00:00:00</span>
</div>
</div>
</div>
<div class="speed-box">
<div class="speed">
<p>1.0<br>倍速</p>
</div>
</div>
</div>
</div>
<script type="text/javascript">
(function($) {
getBarWidth();
getSong();
})(jQuery);
var barWidth;
var ox = 0;
var left = 0;
var oldLeft = 0;
var statu = false;
var newTime = 0;
var progBarLeft = 0;
//UI:获取进度条宽度
function getBarWidth() {
var $barWrapper = $('#bar-wrapper');
var $myAudio = $('#my-audio');
var audioWidth = $myAudio.width();
window.barWidth = (parseInt(audioWidth) - 88);
$barWrapper.width(barWidth);
}
//获取音频资源
function getSong() {
audio = document.getElementById("audio");
audio.src = "http://playback2.duobeiyun.com/jze9087c3cb20148a3b992ea51b438cd75/streams/out-audio-jz38495f9861824801983bc87219637f3f_f_1479106927926_t_1479121820975.m4a";
addAudioEventListener();
checkStorage();
}
//开始监控audio事件
function addAudioEventListener() {
//音频metadata加载完成;UI:显示音频总时间
audio.addEventListener('loadedmetadata', function() {
timeFilter(audio.duration, "audio-duration");
}, false);
//音频正在缓存
audio.addEventListener('progress', function() {
EE_fire("AUDIO_ON_PROGRESS");
}, false);
//播放时间改变;实时保存播放状态
audio.addEventListener('timeupdate', function() {
newTime = audio.currentTime;
localStorage.setItem('storCurTime', audio.currentTime);
setPlyRange(newTime);
setPlyTime(newTime);
}, false);
}
//检测并获取本地保存的时间进度
function checkStorage() {
if (localStorage.getItem('storCurTime')) {
setCurTime();
} else {
addClickListeners();
}
}
//有本地的时间进度时,调整播放时间
function setCurTime() {
audio.addEventListener('loadedmetadata', function() {
var maxWidth = $('#progressBar').width();
var newTime = 0;
console.log("setCurTime", localStorage.getItem('storCurTime'));
audio.currentTime = localStorage.getItem('storCurTime');
newTime = audio.currentTime;
setPlyRange(newTime);
setPlyTime(newTime);
addClickListeners();
}, false);
}
//UI:setCurTime之后改变播放进度条
function setPlyRange(time) {
var $plyRange = $('#played-range');
var newWidth = (time / audio.duration) * barWidth;
console.log("time--", time, "duration--", audio.duration, "barWidth--", barWidth, "newWidth--", newWidth);
$plyRange.width(newWidth);
}
//UI:setCurTime之后改变播放时间
function setPlyTime(time) {
timeFilter(time, "current-time");
}
//UI:显示加载进度条
EE_on("AUDIO_ON_PROGRESS", showBuf);
function showBuf() {
for (var i = audio.buffered.length - 1; i >= 0; i--) {
console.log("buffered ", i, ' -- ', audio.currentTime, ' == ', audio.buffered.start(i), audio.buffered.end(i));
if (audio.buffered.start(i) < audio.currentTime && audio.buffered.end(i) > audio.currentTime) {
var $bufferedRange = $('#buffered-range');
var $plyRange = $('#played-range');
// var endTime = audio.buffered.end(i);
console.log("endTimeI ", i, "audio currentTime: ", audio.currentTime, " buffered start: ", audio.buffered.start(i), " buffered end: ", audio.buffered.end(i));
var bufRange = ((audio.buffered.end(i) - audio.currentTime) / audio.duration) * barWidth;
// console.log("bufRange ", bufRange, audio.currentTime);
$('#buffered-range').width(parseInt(bufRange));
}
}
}
//添加控制事件
function addClickListeners() {
//播放开关点击事件
$('#play-controller').click(function() {
if ($('#play-controller').hasClass('fa-play')) {
$('#play-controller').addClass('fa-pause').removeClass('fa-play');
audio.play();//播放
} else {
$('#play-controller').addClass('fa-play').removeClass('fa-pause');
audio.pause();//暂停
}
});
//mousedown事件;控制audio暂停
$('#bar-wrapper').mousedown(function(e) {
ox = e.pageX - left;
statu = true;
progBarLeft = $('#progress-bar').offset().left;
left = e.pageX - progBarLeft;
limit = parseInt(barWidth) - 1;
if (left < 0) {
left = 0;
}
if (left > barWidth) {
left = barWidth;
}
$('#played-range').width(left);
oldLeft = left;
newTime = (left / barWidth) * audio.duration;
audio.currentTime = newTime;
setPlyTime(newTime);
setPlyRange(newTime);
$('#play-controller').addClass('fa-play').removeClass('fa-pause');
audio.pause();
});
//mousemove事件;控制播放器时间
$('body').mousemove(function(e) {
if (statu) {
left = e.pageX - ox;
//区域判断
if (left < 0) {
left = 0;
}
if (left > barWidth) {
left = barWidth;
}
$('#played-range').width(left);
newTime = (left / barWidth) * audio.duration;
setPlyTime(newTime);
setPlyRange(newTime);
//避免buffer条超出audio
totalWidth = parseInt($('#played-range').width() + $('#buffered-range').width());
if (totalWidth > barWidth) {
$('#buffered-range').width(0);
}
}
});
//mouseup事件
$('#bar-wrapper').mouseup(function() {
$('#play-controller').addClass('fa-pause').removeClass('fa-play');
audio.play();//播放
statu = false;
newTime = (left / barWidth) * audio.duration;
audio.currentTime = newTime;
totalWidth = parseInt($('#played-range').width() + $('#buffered-range').width());
if (totalWidth > barWidth) {
$('#buffered-range').width(0);
}
});
}
//tools
//修改时间的显示格式
function timeFilter(time, timePlace) {
var timePlace = document.getElementById(timePlace);
var hour = time / 3600;
var hours = parseInt(hour);
if (hours < 10) {
hours = "0" + hours;
}
var minute = time / 60 - 60 * hours ;
var minutes = parseInt(minute);
if (minutes < 10) {
minutes = "0" + minutes;
}
var second = time % 60;
seconds = parseInt(second);
if (seconds < 10) {
seconds = "0" + seconds;
}
var allTime = "" + hours + "" + ":" + "" + minutes + "" + ":" + "" + seconds + "";
timePlace.innerHTML = allTime;
}
</script>
</body>
</html>