-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.js
More file actions
155 lines (146 loc) · 5.11 KB
/
player.js
File metadata and controls
155 lines (146 loc) · 5.11 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
window.player = (function() {
var _fsFuncList = ['requestFullscreen',
'webkitRequestFullScreen',
'mozRequestFullScreen',
'msRequestFullScreen']
var _SPEEDBAR_LENGTH = 100
var _time = 0
var _options
var _vWidth
var _doc
var _video
var _posBar
var _durTime
var _bufBar
var _crtTime
var _barLeft
var _crtVol
var _mVideo
var _methods = {
fscreen: function() {
_fsFuncList.forEach(function(item, index) {
if(_video[item]){
_video[item]()
return
}
})
},
play: function(target) {
_video.play()
target.className = _options.pause
target.dataset['click'] = 'pause'
},
pause: function(target) {
_video.pause()
target.className = _options.play
target.dataset['click'] = 'play'
},
setCrtTime: function(target, e) {
_barLeft = target.getBoundingClientRect()['left']
_video.currentTime = (e.clientX - _barLeft) / _vWidth * _video.duration
},
setSpeed: function(target) {
var speed = target.innerHTML
_video.playbackRate = parseFloat(speed)
var speedList = target.parentNode
var crtSpeedBtn = speedList.previousElementSibling
crtSpeedBtn.innerHTML = speed
speedList.style.display = 'none'
},
changeSpeed: function(target) {
var speedList = target.nextElementSibling
if(speedList.style.display){
speedList.style.display = ''
}
else{
speedList.style.display = 'none'
}
},
setVolume: function(target, e) {
if(target){
var barLeft = target.getBoundingClientRect()['left']
_video.volume = (e.clientX - barLeft) / _SPEEDBAR_LENGTH
}
_crtVol.style.width = _video.volume * _SPEEDBAR_LENGTH + 'px'
var iVol = _doc.querySelector('.' + _options.silence)
if(iVol){
iVol.classList.remove(_options.silence)
}
},
setSilence: function(target) {
_video.volume = 0
_crtVol.style.width = 0
target.classList.add(_options.silence)
},
choose: function(target, e) {
e.stopPropagation()
_time = 1
_mVideo.addEventListener('mousemove', _move, false)
_mVideo.addEventListener('mouseup', _release, false)
_mVideo.addEventListener('mouseleave', _release, false)
}
}
var _move = function(e) {
_posBar.style.width = e.clientX - _barLeft + 'px'
}
var _release = function(e) {
_time = 0
_mVideo.removeEventListener('mousemove', _move, false)
_mVideo.removeEventListener('mouseup', _release, false)
_mVideo.removeEventListener('mouseleave', _release, false)
if(e.type === 'mouseup'){
_video.currentTime = (e.clientX - _barLeft) / _vWidth * _video.duration
}
}
//初始化
var init = function(options) {
_doc = document
_options = options
_mVideo = _doc.querySelector(options.mVideo)
_video = _doc.querySelector('.' + options.video)
_video.src = options.src
_crtVol = _doc.querySelector('.' + options.crtVol)
_posBar = _doc.querySelector('.' + options.posBar)
_bufBar = _posBar.parentNode
_crtTime = _doc.querySelector('.' + options.crtTime)
_durTime = _doc.querySelector('.' + options.durTime)
_barLeft = _posBar.getBoundingClientRect()['left']
_mVideo.addEventListener('mousedown', _click, false)
_video.addEventListener('timeupdate', _timeUpdate, false)
_video.addEventListener('loadedmetadata', _loadedmetadata, false)
}
var _click = function(e) {
var target = e.target
var method = target.dataset['click']
if(method){
_methods[method](target, e)
}
}
var _formatTime = function(time) {
time = Math.ceil(time)
var min = parseInt(time / 60)
min = min < 10 ? '0' + min : min
var sec = time % 60
sec = sec < 10 ? '0' + sec : sec
return min + ':' + sec
}
var _timeUpdate = function() {
//当拖动进度条的时候不自动更新时间
if(!_time){
_posBar.style.width = _video.currentTime / _video.duration * _vWidth + 'px'
_bufBar.style.width = _video.buffered.end(0) / _video.duration * _vWidth + 'px'
_crtTime.innerHTML = _formatTime(_video.currentTime)
}
}
var _loadedmetadata = function() {
var _durBar = _bufBar.parentNode
_vWidth = _video.videoWidth
_durTime.innerHTML = _formatTime(_video.duration)
_durBar.style.width = _vWidth + 'px'
_mVideo.style.width = _vWidth + 'px'
_methods.setVolume()
}
return {
init: init
}
})()