index.html
26.2 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>音频</title>
<style type="text/css">
#componentWrapper,
#componentWrapper *{
-webkit-box-sizing: border-box;
box-sizing: border-box;
margin: 0;
padding: 0;
border: 0;
outline: none;
}
#componentWrapper {
position: relative;
height: 40px;
font-family: Arial, Helvetica, sans-serif;
font-size: 15px;
display: -webkit-flex;
display: flex;
}
#componentWrapper .controls_toggle {
position: relative;
width: 40px;
height: 40px;
background: #333;
border-right: 1px solid #555;
border-right-color: rgba( 255, 255, 255, .1);
background-image: url(../images/player_bg2.png);
background-repeat: repeat-x;
-webkit-box-shadow: 1px 1px 6px #333;
box-shadow: 1px 1px 6px #333;
behavior: url(PIE.htc);
cursor: pointer;
}
#componentWrapper .controls_toggle img {
display: block;
position: absolute;
width: 12px;
height: 15px;
left: 50%;
top: 50%;
margin-left: -6px;
margin-top: -8px;
-webkit-user-select: none;
user-select: none;
}
#componentWrapper .player_mediaTime_current {
position: relative;
width: 60px;
height: 40px;
color: white;
text-align: center;
line-height: 40px;
border-left: 1px solid #111;
border-left: 1px solid rgba( 0, 0, 0, .25);
background-image: url(../images/player_bg2.png);
background-repeat: repeat-x;
-webkit-box-shadow: 1px 1px 6px #333;
box-shadow: 1px 1px 6px #333;
behavior: url(PIE.htc);
-webkit-user-select: none;
user-select: none;
}
#componentWrapper .player_progress {
position: relative;
height: 40px;
background-image: url(../images/player_bg2.png);
background-repeat: repeat-x;
-webkit-box-shadow: 1px 1px 6px #333;
box-shadow: 1px 1px 6px #333;
behavior: url(PIE.htc);
cursor: pointer;
flex: 1;
}
#componentWrapper .progress_bg {
position: absolute;
height: 10px;
background: #222;
top: 15px;
left: 10px;
right: 10px;
-webkit-box-shadow: -1px -1px 0 rgba( 0, 0, 0, .5), 1px 1px 0 rgba( 255, 255, 255, .1);
box-shadow: -1px -1px 0 rgba( 0, 0, 0, .5), 1px 1px 0 rgba( 255, 255, 255, .1);
-webkit-border-radius: 4px;
border-radius: 4px;
}
#componentWrapper .load_progress {
position: absolute;
height: 10px;
background: #444;
top: 15px;
left: 10px;
right: 10px;
width: 0px;
-webkit-border-radius: 4px;
border-radius: 4px;
}
#componentWrapper .play_progress {
position: absolute;
height: 10px;
background: #fff;
top: 15px;
left: 10px;
right: 10px;
width: 0px;
-webkit-border-radius: 4px;
border-radius: 4px;
}
#componentWrapper .player_progress_tooltip {
position: relative;
top: -10px;
width: 72px;
background: #333;
height: 16px;
display: none;
text-align: center;
-webkit-box-shadow: 1px 1px 3px #222;
box-shadow: 1px 1px 3px #222;
behavior: url(PIE.htc);
}
#componentWrapper .player_progress_tooltip p {
color: white;
font-size: 10px;
margin: 0;
line-height: 16px;
}
#componentWrapper .player_mediaTime_total {
position: relative;
width: 60px;
height: 40px;
color: white;
text-align: center;
line-height: 40px;
border-right: 1px solid #555;
border-right: 1px solid rgba( 255, 255, 255, .1);
background-image: url(../images/player_bg2.png);
background-repeat: repeat-x;
-webkit-box-shadow: 1px 1px 6px #333;
box-shadow: 1px 1px 6px #333;
behavior: url(PIE.htc);
-webkit-user-select: none;
user-select: none;
}
#componentWrapper .player_volume_wrapper {
position: relative;
height: 40px;
display: -webkit-flex;
display: flex;
}
#componentWrapper .player_volume {
position: relative;
width: 40px;
height: 40px;
border-left: 1px solid #111;
border-left-color: rgba( 0, 0, 0, .25);
background-image: url(../images/player_bg2.png);
background-repeat: repeat-x;
-webkit-box-shadow: 1px 1px 6px #333;
box-shadow: 1px 1px 6px #333;
behavior: url(PIE.htc);
cursor: pointer;
}
#componentWrapper .player_volume img {
display: block;
position: absolute;
width: 18px;
height: 16px;
left: 50%;
top: 50%;
margin-left: -9px;
margin-top: -8px;
-webkit-user-select: none;
user-select: none;
}
#componentWrapper .volume_seekbar {
position: relative;
width: 80px;
height: 40px;
background-image: url(../images/player_bg2_vol.png);
background-repeat: repeat-x;
-webkit-box-shadow: 1px 1px 6px #333;
box-shadow: 1px 1px 6px #333;
behavior: url(PIE.htc);
/*display: none;*/
cursor: pointer;
}
#componentWrapper .volume_bg {
position: absolute;
width: 60px;
height: 10px;
background: #222;
bottom: 15px;
left: 10px;
-webkit-box-shadow: -1px -1px 0 rgba( 0, 0, 0, .5), 1px 1px 0 rgba( 255, 255, 255, .1);
box-shadow: -1px -1px 0 rgba( 0, 0, 0, .5), 1px 1px 0 rgba( 255, 255, 255, .1);
-webkit-border-radius: 4px;
border-radius: 4px;
}
#componentWrapper .volume_level {
position: absolute;
width: 30px;
height: 10px;
background: #fff;
bottom: 15px;
left: 10px;
-webkit-border-radius: 4px;
border-radius: 4px;
}
#componentWrapper .player_volume_tooltip {
position: relative;
left: -15px;
top: -4px;
width: 35px;
height: 16px;
background: #333;
display: none;
text-align: center;
-webkit-box-shadow: 1px 1px 3px #222;
box-shadow: 1px 1px 3px #222;
behavior: url(PIE.htc);
}
#componentWrapper .player_volume_tooltip p {
font-family: Arial, Helvetica, sans-serif;
font-size: 10px;
color: #ffffff;
margin: 0;
line-height: 18px;
}
</style>
</head>
<body>
<style type="text/css">
li a {
line-height: 2;
text-indent: 1em;
color: #00f;
display: block;
}
li a:hover {
background-color: #dcdcdc;
}
</style>
<ul>
<li><a href="javascript:;" data-url="./Fade.mp3">Fade</a></li>
<li><a href="javascript:;" data-url="./failed.mp3">failed</a></li>
<li><a href="javascript:;" data-url="./yinweiaiqing.mp4">因为爱情</a></li>
<li><a href="javascript:;" data-url="./tonghuazhen.mp3">童话镇</a></li>
<li><a href="javascript:;" data-url="./miaolingtong.mp3">妙龄童</a></li>
</ul>
<div id="audioWrap"></div>
<script type="text/javascript" src="../lib/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
var AUDIO = {
options: {
debuggers: false, // 是否开启播放记录
container: 'body', // 把组件塞到什么地方
source: '', // 音频源文件
seekNum: 0, // 拖拽是判断之前是否播放
allowSeek: true, // 是否可以拖拽
canplayCallback: null, // 可以播放之后,做某些事情
playCallback: null, // 播放之后,做某些事情
pauseCallback: null, // 暂停之后,做某些事情
timeupdateCallback: null, // 拖动之后,做某些事情
endedCallback: null, // 播放结束之后,做某些事情
mutedCallback: null, // 静音之后,做某些事情
template: $('<div id="componentWrapper">' +
'<audio id="audio-element" style="display: none;">您的浏览器不支持 audio 标签。</audio>' +
'<div class="controls_toggle"><img src="../images/play.png" alt="controls_toggle" /></div>' +
'<div class="player_mediaTime_current">00:00</div>' +
'<div class="player_progress">' +
'<div class="progress_bg"></div>' +
'<div class="load_progress"></div>' +
'<div class="play_progress"></div>' +
'<div class="player_progress_tooltip">' +
'<p></p>' +
'</div>' +
'</div>' +
'<div class="player_mediaTime_total">00:00</div>' +
'<div class="player_volume_wrapper">' +
'<div class="player_volume"><img src="../images/volume.png" alt="player_volume" /></div>' +
'<div class="volume_seekbar">' +
'<div class="volume_bg"></div>' +
'<div class="volume_level"></div>' +
'<div class="player_volume_tooltip">' +
'<p></p>' +
'</div>' +
'</div>' +
'</div>' +
'</div>')
},
elements: {},
init: function(options) {
var _this = this;
if (!options || !options.container) {
return false;
}
this.options = $.extend(this.options, options);
// audio DOM
_this.elements.audioDom = _this.options.template.find('audio')[0];
// 播放按钮
_this.elements.playButton = _this.options.template.find('.controls_toggle');
// 当前时间
_this.elements.currentTime = _this.options.template.find('.player_mediaTime_current');
// 总时长
_this.elements.totalTime = _this.options.template.find('.player_mediaTime_total');
// 加载进度条
_this.elements.loadProgress = _this.options.template.find('.load_progress');
// 播放进度条
_this.elements.playProgress = _this.options.template.find('.play_progress');
// 播放进度条wrap
_this.elements.playProgressWrap = _this.options.template.find('.player_progress');
// 播放进度条总长
_this.elements.totalPlayProgress = _this.options.template.find('.progress_bg');
// 静音按钮
_this.elements.mutedButton = _this.options.template.find('.player_volume');
// 音量大小wrap
_this.elements.volumeWrap = _this.options.template.find('.volume_seekbar');
// 音量总长
_this.elements.totalVolume = _this.options.template.find('.volume_bg');
// 当前音量
_this.elements.currentVolume = _this.options.template.find('.volume_level');
// 音量提示条
_this.elements.tipsVolume = _this.options.template.find('.player_volume_tooltip');
$(_this.options.container).append(_this.options.template);
if(options.source){
_this.updateSource({source: options.source, callback: function(){ _this.log('update source') }});
}
_this.elements.currentVolume.width((60 / 100 * _this.elements.audioDom.volume * 100) + 'px');
// 初始化话事件
_this.events();
},
updateSource: function(o){
var _this = this;
// 重置
_this.reset();
// 更新音频来源
_this.elements.audioDom.setAttribute('src', o.source);
if(typeof o.callback == 'function'){
o.callback();
}
},
events: function() {
var _this = this;
// 调节声音大小
_this.elements.volumeWrap.on("mouseenter", function(event) {
event.preventDefault();
}).on("mouseleave", function(event) {
event.preventDefault();
_this.elements.tipsVolume.css('display', 'none');
_this.setvolume();
}).on('mousemove', function(event) {
event.preventDefault();
if ((event.pageX - _this.elements.totalVolume.offset().left) > 0 && (event.pageX - _this.elements.totalVolume.offset().left) < (_this.elements.totalVolume.width() + 1)) {
_this.elements.tipsVolume.css({
'display': 'block',
"left": parseInt(event.pageX - _this.elements.totalVolume.offset().left + _this.elements.totalVolume.width() / 2 - 40, 10) + "px"
});
_this.elements.tipsVolume.find("p").html(_this.sumVolume(event) + " %")
_this.elements.currentVolume.width(event.pageX - _this.elements.totalVolume.offset().left + "px");
}
}).on('mouseup', function(event) {
event.preventDefault();
_this.elements.currentVolume.width(event.pageX - _this.elements.totalVolume.offset().left + "px");
_this.setvolume({
event: event
});
});
_this.elements.mutedButton.on('click', function(event) {
event.preventDefault();
/* Act on the event */
_this.muted();
});
// 播放进度条
if(_this.options.allowSeek){
_this.elements.playProgressWrap.on('mousedown', function(event) {
event.preventDefault();
if (_this.elements.audioDom.paused) {
_this.options.seekNum = 1;
} else {
_this.options.seekNum = 2;
_this.pause();
}
// 鼠标按下就更新进度条
_this.setPlayProgressWidth(event);
}).on('mousemove', function(event) {
event.preventDefault();
// 判断是否已经开始移动 才会设置进度条
_this.options.seekNum != 0 && _this.setPlayProgressWidth(event);
}).on('mouseup', function(event) {
event.preventDefault();
// TODO 是否有必要在鼠标按键抬起的时候再次去更新播放进度条宽度
// _this.setPlayProgressWidth(event);
if (_this.options.seekNum == 1) {
_this.pause();
} else {
_this.play();
}
_this.options.seekNum = 0;
if(typeof _this.options.seekedCallback == 'function'){
_this.options.seekedCallback({'status': _this.elements.audioDom.seeked});
}
})
}else{
_this.elements.playProgressWrap.on('mousedown', function(event) {
event.preventDefault();
if(typeof _this.options.seekedCallback == 'function'){
_this.options.seekedCallback();
}else{
alert('暂时不支持拖拽');
return false;
}
})
}
// 播放暂停
_this.elements.playButton.on('click', function(event) {
event.preventDefault();
_this.toggleplay({
playCallback: function(){ _this.log('toggleplay:play') },
pauseCallback: function(){ _this.log('toggleplay:pause') }
});
})
// 当音频的加载已放弃时
_this.elements.audioDom.onabort = function() {
_this.log('onabort');
_this.reset();
}
// 当浏览器可以播放音频时
_this.elements.audioDom.oncanplay = function() {
_this.log('oncanplay');
// 判断音频加载完毕
if (_this.elements.audioDom.readyState > 0) {
var duration = Math.round(_this.elements.audioDom.duration);
var minutes = Math.floor(duration / 60);
minutes = 10 <= minutes ? minutes : "0" + minutes;
duration = Math.floor(duration % 60);
_this.elements.totalTime.html(minutes + ":" + (10 <= duration ? duration : "0" + duration));
if(typeof _this.options.canplayCallback == 'function'){
_this.options.canplayCallback({'status': true});
}
}
}
// 当浏览器正在下载音频时
_this.elements.audioDom.onprogress = function() {
_this.elements.loadProgress.width((_this.elements.audioDom.buffered.end(0) / _this.elements.audioDom.seekable.end(0)) * _this.elements.totalPlayProgress.width());
};
// 当浏览器开始查找音频时
_this.elements.audioDom.onloadstart = function() {
_this.log('onloadstart');
}
// 当音频已开始或不再暂停时
_this.elements.audioDom.onplay = function() {
_this.log('onplay');
_this.elements.playButton.find('img').attr('src', '../images/pause.png');
}
// 当音频已暂停时
_this.elements.audioDom.onpause = function() {
_this.log('onpause');
_this.elements.playButton.find('img').attr('src', '../images/play.png');
}
// 当目前的播放列表已结束时
_this.elements.audioDom.onended = function() {
_this.log('onended');
if(typeof _this.options.endedCallback == 'function'){
_this.options.endedCallback({'status': _this.elements.audioDom.ended});
}
}
// 当用户开始移动/跳跃到音频中的新位置时
_this.elements.audioDom.onseeking = function() {
_this.log('onseeking');
}
// 当用户已移动/跳跃到音频中的新位置时
_this.elements.audioDom.onseeked = function() {
_this.log('onseeked');
}
// 当目前的播放位置已更改时
_this.elements.audioDom.ontimeupdate = function() {
_this.log('ontimeupdate');
_this.timeupdate();
}
// 当音量已更改时
_this.elements.audioDom.onvolumechange = function() {
_this.log('onvolumechange');
_this.setvolume();
}
},
toggleplay: function(o) {
var _this = this;
if (_this.elements.audioDom.paused) {
_this.play(o.playCallback);
} else {
_this.pause(o.pauseCallback);
}
},
play: function(o) {
var _this = this;
_this.elements.audioDom.play();
if (typeof o == 'function') {
o({'status': _this.elements.audioDom.play});
}
if(typeof _this.options.playCallback == 'function'){
_this.options.playCallback({'status': _this.elements.audioDom.play});
}
},
pause: function(o) {
var _this = this;
_this.elements.audioDom.pause();
if (typeof o == 'function') {
o({'status': _this.elements.audioDom.play});
}
if(typeof _this.options.pauseCallback == 'function'){
_this.options.pauseCallback({'status': _this.elements.audioDom.play});
}
},
// 设置静音否
muted: function() {
var _this = this;
if (_this.elements.audioDom.muted) {
_this.elements.audioDom.muted = false;
_this.elements.mutedButton.find('img').attr('src', '../images/volume.png');
} else {
_this.elements.audioDom.muted = true;
_this.elements.mutedButton.find('img').attr('src', '../images/mute.png');
}
if(typeof _this.options.mutedCallback == 'function'){
_this.options.mutedCallback({'status': _this.elements.audioDom.muted});
}
},
// 设置声音大小
setvolume: function(arguments) {
var _this = this;
if (!arguments) {
_this.elements.currentVolume.width((_this.elements.totalVolume.width() / 100 * _this.elements.audioDom.volume * 100) + 'px');
} else {
if (arguments.event) {
return _this.elements.audioDom.volume = _this.sumVolume(arguments.event) / 100;
} else {
// _this.log 需要优化
_this.options.debuggers && console.error('缺少参数:arguments.event');
return false;
}
}
},
// 设置播放进度条
setPlayProgressWidth: function(argument) {
var _this = this;
if (!argument) {
return false;
}
if ((argument.clientX - _this.elements.playProgressWrap.offset().left) > _this.elements.loadProgress.width()) {
_this.elements.playProgress.width(_this.elements.loadProgress.width());
} else {
_this.elements.playProgress.width(argument.clientX - _this.elements.playProgressWrap.offset().left);
_this.elements.audioDom.currentTime = (argument.clientX - _this.elements.playProgressWrap.offset().left) / _this.elements.totalPlayProgress.width() * _this.elements.audioDom.duration;
}
},
// 播放时间更改
timeupdate: function() {
var _this = this;
var currTime = Math.floor(_this.elements.audioDom.currentTime).toString();
var duration = Math.floor(_this.elements.audioDom.duration).toString();
_this.elements.currentTime.html(_this.formatTime(currTime));
if (isNaN(duration)) {
_this.elements.totalTime.html('00:00');
} else {
_this.elements.totalTime.html(_this.formatTime(duration));
}
_this.elements.playProgress.width((_this.elements.audioDom.currentTime / _this.elements.audioDom.duration) * $('.progress_bg').width());
},
// 格式化时间戳相关
formatTime: function(secs, format) {
var hr = Math.floor(secs / 3600);
var min = Math.floor((secs - (hr * 3600)) / 60);
var sec = Math.floor(secs - (hr * 3600) - (min * 60));
if (min < 10) {
min = "0" + min;
}
if (sec < 10) {
sec = "0" + sec;
}
return min + ':' + sec;
},
// 计算音量
sumVolume: function(event) {
var _this = this;
if (!event) {
return false;
}
var num = event.pageX - _this.elements.totalVolume.offset().left;
num = Math.max(0, Math.min(1, num / _this.elements.totalVolume.width()));
num = parseInt(100 * num, 10);
return num;
},
// 重置函数
reset: function(){
var _this = this;
// 播放按钮 当前播放时间 总时间 音量 播放进度条 下载进度条
_this.elements.playButton.find('img').attr('src', '../images/play.png');
_this.elements.currentTime.html('00:00');
_this.elements.totalTime.html('00:00');
_this.elements.currentVolume.width(_this.elements.totalVolume);
_this.elements.playProgress.width(0);
_this.elements.loadProgress.width(0);
},
log: function(log){
var _this = this;
if(_this.options.debuggers){
return console.info(log);
}
}
}
$(function() {
$('[data-url]').on('click', function(event) {
event.preventDefault();
/* Act on the event */
AUDIO.updateSource({
source: $(this).data('url'),
callback: function(){
}
});
// AUDIO.play();
});
AUDIO.init({
container: '#audioWrap'
,source: './Fade.mp3'
,debuggers: false
,allowSeek: false
,canplayCallback: function(){ console.log('canplayCallback') }
,playCallback: function(){ console.log('playCallback') }
,pauseCallback: function(){ console.log('pauseCallback') }
,seekedCallback: function(){ alert('认真一点了,快进也许会丢失一些精彩'); }
,endedCallback: function(){ console.log('endedCallback') }
,mutedCallback: function(data){ console.log(data) }
});
});
</script>
</body>
</html>