-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacplayer.js
151 lines (142 loc) · 4.88 KB
/
acplayer.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
149
150
151
/*
AUTHOR: Osvaldas Valutis, www.osvaldas.info
*/
//说明js是加载是正确的,接下来看看是哪里出了问题
;(function( $, window, document, undefined )
{
'use strict'
var isTouch = 'ontouchstart' in window,
eStart = isTouch ? 'touchstart' : 'mousedown',
eMove = isTouch ? 'touchmove' : 'mousemove',
eEnd = isTouch ? 'touchend' : 'mouseup',
eCancel = isTouch ? 'touchcancel' : 'mouseup',
secondsToTime = function( secs )
{
var hours = Math.floor( secs / 3600 ), minutes = Math.floor( secs % 3600 / 60 ), seconds = Math.ceil( secs % 3600 % 60 );
return ( hours == 0 ? '' : hours > 0 && hours.toString().length < 2 ? '0'+hours+':' : hours+':' ) + ( minutes.toString().length < 2 ? '0'+minutes : minutes ) + ':' + ( seconds.toString().length < 2 ? '0'+seconds : seconds );
};
var AcPlayer = function(ele, opt) {
this.$element = ele,
this.theAudio = ele.get(0),
this.$playpause = $('#playpause'),
this.$imgRotate = $("#img-rotate"),
this.currentTime = 0,
this.defaults = {
url:''
},
this.params = $.extend({}, this.defaults, opt),
this.audioState='paused';
}
AcPlayer.prototype = {
play:function(){
if( this.$playpause.hasClass('acplayer-paused') )
{
this.$playpause.removeClass('acplayer-paused').addClass('acplayer-playing');
}
if(!this.$imgRotate.hasClass('rotate-img'))
{
this.$imgRotate.addClass('rotate-img');
}
this.$imgRotate.css('webkitAnimationPlayState','running');
if (this.theAudio.paused) {
this.theAudio.play();
this.monitorCurrentTime();
this.audioState='playing';
}
},
pause:function(){
if( this.$playpause.hasClass('acplayer-playing') )
{
this.$playpause.removeClass('acplayer-playing').addClass('acplayer-paused');
}
clearInterval(this.ivl);
if (!this.theAudio.paused) {
this.theAudio.pause();
this.$imgRotate.css('webkitAnimationPlayState','paused');
this.audioState='paused';
}
},
monitorCurrentTime:function(){
var that = this;
clearInterval(this.ivl);
this.ivl = setInterval(function(){
that.currentTime = that.theAudio.currentTime;
console.log(that.currentTime)
return that.currentTime;
},100)
},
getCurrentTime:function(){
return that.theAudio.currentTime;
},
toTheTime:function(time){
if(time <= that.theAudio.duration)
{
that.theAudio.currentTime = time;
}
},
init: function() {
var that = this;
this.$element.each( function()
{
that.theAudio = $(this).clone().get(0);
var timeCurrent = $('.audioplayer-time-current'),
timeDuration = $('.audioplayer-time-duration'),
barLoaded = $('.audioplayer-bar-loaded'),
barPlayed=$('.audioplayer-bar-played'),
theBar=$('.audioplayer-bar'),
theAudio = that.theAudio,
adjustCurrentTime = function( e )
{
var theRealEvent = isTouch ? e.originalEvent.touches[ 0 ] : e;
theAudio.currentTime = Math.round( ( theAudio.duration * ( theRealEvent.pageX - theBar.offset().left ) ) / theBar.width() );
},
updateLoadBar = function()
{
var interval = setInterval( function()
{
if( theAudio.buffered.length < 1 ) return true;
barLoaded.width( ( theAudio.buffered.end( 0 ) / theAudio.duration ) * 100 + '%' );
if( Math.floor( theAudio.buffered.end( 0 ) ) >= Math.floor( theAudio.duration ) ) clearInterval( interval );
}, 100 );
};
timeDuration.html('…');
timeCurrent.html( secondsToTime( 0 ) );
theAudio.addEventListener('loadeddata', function()
{
updateLoadBar();
timeDuration.html( $.isNumeric( theAudio.duration ) ? secondsToTime( theAudio.duration ) : '…' );
});
theAudio.addEventListener( 'timeupdate', function()
{
timeCurrent.html( secondsToTime( theAudio.currentTime ) );
barPlayed.width( ( theAudio.currentTime / theAudio.duration ) * 100 + '%' );
});
theAudio.addEventListener('ended', function() {
that.$imgRotate.css({
'webkitAnimationPlayState':'paused'
});
that.$imgRotate.removeClass('rotate-img');
that.$playpause.removeClass('acplayer-playing').addClass('acplayer-paused');
that.audioState='paused';
}, true);
theBar.on( eStart, function( e )
{
adjustCurrentTime( e );
theBar.on( eMove, function( e ) { adjustCurrentTime( e ); } );
}).on( eCancel, function()
{
theBar.unbind( eMove );
});
$(this).replaceWith( theAudio );
});
return this;
}
};
$.fn.acPlayer = function( options )
{
//创建Beautifier的实体
var acPlayer = new AcPlayer(this, options);
//调用其方法
return acPlayer.init();
}
})( jQuery, window, document );