MediaControlBarApe.js
11.6 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
//*
// 媒体控制栏
// */
import Loger from "../Loger";
import Ape from "./Ape";
import $ from "jquery";
import ClassDataProxy from "proxy/ClassDataProxy";
import TimerCounterTool from "libs/TimerCounterTool";
let loger = Loger.getLoger('PC-MediaControlBarApe');
const SOUND_BTN_WIDTH=66;
const TIME_TEXT_WIDTH=90;
class MediaControlBarApe extends Ape {
constructor() {
super();
this.playStatus = "";
this.isShowVolumeBar = false;//
this.isProgressMouseDown = false;//进度条是否按下
this.isProVolMouseDown = false;//音量控制条是否已经按钮
this.isOpenUpdateStatusInfo = false;//是否开同步信息
this.video = null;//需要控制的播放器
this.controlCallback = null;
this.totalTime = 0;
this.duration=0;//媒体文件的总时间长
this.mediaVolume = 60;//当前音量
this.seekPercent = 0;//当前进度
this.seekTime = 0;//
this.updateCounter = 0;//定时更新数据的计数器
this.updateStatusDelay = 5;//每5秒更新一次数据
this.timerCounter = new TimerCounterTool();//计时器
this.timerCounter.addTimerCallBack(this.timerCounterUptate.bind(this), 1);//计时器监听 每秒更新一次
this.init();
this.addEvent();
this.onWindowResize();//默认刷新一次界面布局
}
init() {
this.stop();
this.updatePlayProgressBar();
this.updaterVolumeProgressBar()
}
addEvent() {
//监听舞台
$(window).on('resize', this.onWindowResize.bind(this));
$("#mediaShareVolumePlay").on("click", this._openVolumeControl.bind(this));//打开音量控制
//音量控制
$('#mediaShareVoice_btn').on('mousedown', this._changeVolume.bind(this));//音量进度条
//暂停播放伴音按钮
$("#mediaShareProgressPause").on("click", this._clickPause.bind(this));
//继续播放伴音按钮
$("#mediaShareProgressPlay").on("click", this._clickPlay.bind(this));
//进度条控制
$('#mediaShareProgress_btn').on('mousedown', this._changeProgressBar.bind(this));
//给整个进度条可以点击seek
$('#mediaShareProgress_bg').on('mousedown', this._clickProgressBarBg.bind(this));
$('#mediaShareProgress_color').on('mousedown', this._clickProgressBarBg.bind(this));
}
//计时器-------------------
//开启计时器
startTimerCounter() {
loger.log("开启计时器")
this.updateCounter = 0;
this.stopTimerCounter();
if (this.timerCounter) {
this.timerCounter.startTimer();
}
}
//停止计时器
stopTimerCounter() {
loger.log("停止计时器")
this.updateCounter = 0;
if (this.timerCounter) {
this.timerCounter.stopTimer();
}
}
timerCounterUptate() {
if (this.video && !this.isProgressMouseDown) {
this.totalTime = this.video.duration || this.duration;
this.seekTime = this.video.currentTime || 0;
this.seekPercent = parseInt(this.seekTime / this.totalTime * 100);
//console.log("totalTime", this.totalTime)
//console.log("seekTime", this.seekTime)
//console.log("seekPercent", this.seekPercent)
this.updatePlayProgressBar();
}
if (this.seekTime >= this.totalTime&&this.seekTime!=1) {
if (this.totalTime > 0) {
this.sendCallBack(MediaControlBarApe.STOP)
}
this.stopTimerCounter();
this.stop();
}
//定时更新同步数据
this.updateCounter++;
if (this.updateCounter >= this.updateStatusDelay) {
this.updateCounter = 0;
loger.warn("定时同步更新媒体共享的状态")
this.sendCallBack(this.playStatus);
}
}
//外部接口调用------------------------------
play(_seek) {
loger.log("play");
this.playStatus = MediaControlBarApe.PLAY;
this.showPauseUI();
this.showBox();
this.startTimerCounter();
}
pause() {
loger.log("pause");
this.playStatus = MediaControlBarApe.PAUSE;
this.showPlayUI();
this.showBox();
this.stopTimerCounter();
}
stop() {
loger.log("stop");
this.seekPercent = 0;
this.seekTime = 0;
this.totalTime = 0;
this.playStatus = MediaControlBarApe.STOP;
this._hideMediaSharePlayProgressVoiceBox();
this.updatePlayProgressBar();
this.showPlayUI();
this.hideBox();
this.stopTimerCounter();
}
seek(_seek) {
loger.log("seek");
this.showBox();
this.seekTime = _seek || 0;
this.seekPercent = parseInt(this.seekTime / this.totalTime * 100);
this.updateVideoProgress();
this._hideMediaSharePlayProgressVoiceBox();
}
changeVolume(_volume) {
loger.log("changeVolume");
this.showBox();
this.mediaVolume = _volume || 0;
this.updaterVolumeProgressBar();
}
//-UI
showPlayUI() {
$("#mediaShareProgressPause").hide();
$("#mediaShareProgressPlay").show();
}
showPauseUI() {
$("#mediaShareProgressPause").show();
$("#mediaShareProgressPlay").hide();
}
showBox() {
$("#h5MediaShareControls").show();
}
hideBox() {
$("#h5MediaShareControls").hide();
}
//------------------------------------
//播放按钮
_clickPlay() {
loger.log("播放按钮点击===>");
this.updateCounter = 0;
this.playStatus = MediaControlBarApe.PLAY;
this._hideMediaSharePlayProgressVoiceBox();
this.showPauseUI();
this.startTimerCounter();
this.sendCallBack(MediaControlBarApe.PLAY);
}
//暂停播放
_clickPause() {
loger.log("暂停播放按钮点击===>");
this.updateCounter = 0;
this.playStatus = MediaControlBarApe.PAUSE;
this._hideMediaSharePlayProgressVoiceBox();
this.showPlayUI();
this.stopTimerCounter();
this.sendCallBack(MediaControlBarApe.PAUSE);
}
//打开音量控制条
_openVolumeControl() {
if (this.isShowVolumeBar == true) {
this._hideMediaSharePlayProgressVoiceBox();
} else {
this._showMediaSharePlayProgressVoiceBox();
}
}
//音量进度条-----------------------------------------
//音量控制鼠标按下
_changeVolume(evt) {
this.updateCounter = 0;
this.isProVolMouseDown = true;
document.onmousemove = this._mouseMoveMediaVoiceHandler.bind(this);
document.onmouseup = this._mouseUpMediaVoiceHandler.bind(this);
return false;
}
//音量条移动
_mouseMoveMediaVoiceHandler(evt) {
let per = this._moveVolMethod(evt);
this.mediaVolume = per;
this.updaterVolumeProgressBar();
}
_mouseUpMediaVoiceHandler(evt) {
if (this.isProVolMouseDown) {
if (this.playStatus == MediaControlBarApe.PLAY) {
this.sendCallBack(MediaControlBarApe.PLAY)
} else if (this.playStatus == MediaControlBarApe.PAUSE) {
this.sendCallBack(MediaControlBarApe.PAUSE)
} else {
this.sendCallBack(MediaControlBarApe.STOP)
}
}
this.isProVolMouseDown = false;
document.onmousemove = null;
document.onmouseup = null
}
//音量移动函数
_moveVolMethod(evt) {
let windowY = (evt || window.event).clientY;
let _width = $('#mediaShareVoice_bg')
let top = _width.offset().top;
let _percentage = _width.innerHeight();
let prevX = parseInt(windowY - top);
if (prevX < 0)prevX = 0;
if (prevX > _percentage)prevX = _percentage;
let per = Math.floor(100 * prevX / _percentage);
per = 100 - per;
return per;
}
updaterVolumeProgressBar() {
let _color = $('#mediaShareVoice_color');
let _btn = $('#mediaShareVoice_btn');
let perChange = this.mediaVolume / 100;
if (this.video) {
this.video.volume = perChange;//设置音量
}
//let curSeek = document.getElementById("h5MediaShare").currentTime;
_btn.css('top', 100 - this.mediaVolume + '%');
_color.css('height', (this.mediaVolume) + '%');
}
_showMediaSharePlayProgressVoiceBox() {
this.isShowVolumeBar = true;
$(".mediaSharePlayProgressVoiceBox").show();
}
_hideMediaSharePlayProgressVoiceBox() {
this.isShowVolumeBar = false;
$(".mediaSharePlayProgressVoiceBox").hide();
}
//播放进度条控制-------------------------------------
_changeProgressBar(evt) {
this._hideMediaSharePlayProgressVoiceBox();
this.isProgressMouseDown = true;
document.onmousemove = this._mouseMoveMediaProgressHandler.bind(this);
document.onmouseup = this._mouseUpMediaaHandler.bind(this);
return false;
}
_clickProgressBarBg(evt) {
this.isProgressMouseDown=true;
let per = this._moveMethod(evt);
this.seekPercent = per;
this.updatePlayProgressBar();
this. _mouseUpMediaaHandler(evt);
}
//鼠标在进度条上移动
_mouseMoveMediaProgressHandler(evt) {
let per = this._moveMethod(evt);
this.seekPercent = per;
this.updatePlayProgressBar();
//console.log("进度条移动百分比", this.seekPercent);
}
updatePlayProgressBar() {
let _color = $('#mediaShareProgress_color');
let _btn = $('#mediaShareProgress_btn');
_btn.css('left', this.seekPercent + '%');
_color.css('width', this.seekPercent + '%');
let curTimeStr=this.timerCounterLayOut(this.seekTime);
let totalTimeStr=this.timerCounterLayOut(this.totalTime);
$("#mediaShareTimestamp").html(curTimeStr+"/"+totalTimeStr);
}
updateVideoProgress() {
if (this.video) {
try{
this.video.currentTime = this.seekTime;//设置音量
}catch (err){
loger.warn("无法设置video的currentTime");
}
}
}
//鼠标进度条抬起
_mouseUpMediaaHandler(evt) {
if (this.isProgressMouseDown) {
this.seekTime = parseInt(this.seekPercent * this.totalTime * 0.01);
if (this.playStatus == MediaControlBarApe.PLAY) {
this.sendCallBack(MediaControlBarApe.PLAY)
} else if (this.playStatus == MediaControlBarApe.PAUSE) {
this.sendCallBack(MediaControlBarApe.PAUSE)
} else {
this.sendCallBack(MediaControlBarApe.STOP)
}
this.updateVideoProgress();
}
this.isProgressMouseDown = false;
document.onmousemove = null;
document.onmouseup = null
}
//进度条移动函数
_moveMethod(evt) {
let windowX = (evt || window.event).clientX;
let _width = $('#mediaShareProgress_bg')
let left = _width.offset().left;
let _percentage = _width.innerWidth();
let prevX = parseInt(windowX - left);
if (prevX < 0)prevX = 0;
if (prevX > _percentage)prevX = _percentage;
let per = Math.floor(100 * prevX / _percentage);
return per;
}
timerCounterLayOut(timestamp) {
let minute = 0,
second = 0;
minute = Math.floor(timestamp / 60);
second = Math.floor(timestamp - minute * 60)
let timeValue = ((minute < 10) ? "0" : "") + minute;
timeValue += ((second < 10) ? ":0" : ":") + second;
return timeValue;
}
//舞台大小发生改变
onWindowResize() {
//loger.log("舞台大小发生改变");
let boxWidth = $(".h5MediaShareControls").width();
//进度条的宽度是盒子的总宽度减去 声音按钮和时间显示区域的宽度
$(".mediaSharePlayProgressBox").width((boxWidth - SOUND_BTN_WIDTH-TIME_TEXT_WIDTH) + "px")
}
//调用回调函数通知外部
sendCallBack(_action, _data) {
if (!this.isOpenUpdateStatusInfo) {
return
}
if (this.controlCallback) {
this.controlCallback({
action: _action,
data: {
seek: this.seekTime,
mediaVolume: this.mediaVolume
}
})
}
}
}
MediaControlBarApe.prototype.STOP = MediaControlBarApe.STOP = "stop";//0
MediaControlBarApe.prototype.PLAY = MediaControlBarApe.PLAY = "play";//1
MediaControlBarApe.prototype.PAUSE = MediaControlBarApe.PAUSE = "pause";//2
MediaControlBarApe.prototype.SEEK = MediaControlBarApe.SEEK = "seek";//3
MediaControlBarApe.prototype.CHANGE_VOLUME = MediaControlBarApe.CHANGE_VOLUME = "change_volume";//4
export default MediaControlBarApe;