-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathjquery.simpleplayer.js
103 lines (90 loc) · 4.26 KB
/
jquery.simpleplayer.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
/*
* SimplePlayer - A jQuery Plugin
* @requires jQuery v1.4.2 or later
*
* SimplePlayer is a html5 audio player
*
* Licensed under the MIT:
* http://www.opensource.org/licenses/mit-license.php
*
* Copyright (c) 2010-, Yuanhao Li (jay_21cn -[at]- hotmail [*dot*] com)
*/
(function($) {
$.fn.player = function(settings) {
var config = {
progressbarWidth: '200px',
progressbarHeight: '5px',
progressbarColor: '#22ccff',
progressbarBGColor: '#eeeeee',
defaultVolume: 0.8
};
if (settings) {
$.extend(config, settings);
}
var playControl = '<span class="simpleplayer-play-control"></span>';
var stopControl = '<span class="simpleplayer-stop-control"></span>';
this.each(function() {
$(this).before('<div class="simple-player-container" style="background-color: #ddd; padding: 0 10px 5px 5px;">');
$(this).after('</div>');
$(this).parent().find('.simple-player-container').prepend(
'<div><ul>' +
'<li style="display: inline-block; padding: 0 5px; "><a style="text-decoration: none;"' +
' class="start-button" href="javascript:void(0)">' + playControl + '</a></li>' +
'<li class="progressbar-wrapper" style="display: inline-block; cursor: pointer; width:' + config.progressbarWidth + ';">' +
'<span style="display: block; background-color: ' + config.progressbarBGColor + '; width: 100%; ">' +
'<span class="progressbar" style="display: block; background-color: ' + config.progressbarColor +
'; height: ' + config.progressbarHeight + '; width: 0%; ">' +
'</span></span>' +
'</li>' +
'</ul></div>'
);
var simplePlayer = $(this).get(0);
var button = $(this).parent().find('.start-button');
var progressbarWrapper = $(this).parent().find('.progressbar-wrapper');
var progressbar = $(this).parent().find('.progressbar');
simplePlayer.volume = config.defaultVolume;
button.click(function() {
if (simplePlayer.paused) {
/*stop all playing songs*/
$.each($('audio'), function () {
this.pause();
$(this).parent().find('.simpleplayer-stop-control').addClass('simpleplayer-play-control').removeClass('simpleplayer-stop-control');
});
simplePlayer.play();
$(this).find('.simpleplayer-play-control').addClass('simpleplayer-stop-control').removeClass('simpleplayer-play-control');
} else {
simplePlayer.pause();
$(this).find('.simpleplayer-stop-control').addClass('simpleplayer-play-control').removeClass('simpleplayer-stop-control');
}
});
progressbarWrapper.click(function(e) {
if (simplePlayer.duration != 0) {
left = $(this).offset().left;
offset = e.pageX - left;
percent = offset / progressbarWrapper.width();
duration_seek = percent * simplePlayer.duration;
simplePlayer.currentTime = duration_seek;
}
});
$(simplePlayer).bind('ended', function(evt) {
simplePlayer.pause();
button.find('.simpleplayer-stop-control').addClass('simpleplayer-play-control').removeClass('simpleplayer-stop-control');
progressbar.css('width', '0%');
});
$(simplePlayer).bind('timeupdate', function(e) {
duration = this.duration;
time = this.currentTime;
fraction = time / duration;
percent = fraction * 100;
if (percent) progressbar.css('width', percent + '%');
});
if (simplePlayer.duration > 0) {
$(this).parent().css('display', 'inline-block');
}
if ($(this).attr('autoplay') == 'autoplay') {
button.click();
}
});
return this;
};
})(jQuery);