forked from bitmovin/bitmovin-player-web-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerKeyboardControl.ts
503 lines (458 loc) · 16.5 KB
/
PlayerKeyboardControl.ts
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
/****************************************************************************
* Copyright (C) 2017, Bitmovin, Inc., All Rights Reserved
*
* This source code and its use and distribution, is subject to the terms
* and conditions of the applicable license agreement.
****************************************************************************/
const bitmovin = window.bitmovin;
/**
* Default implementation of the KeyMap to Control the player
*/
class DefaultPlayerKeymap implements PlayerKeyMap {
toggle_play = <KeyToFunctionBinding>{
keyBinding: 'space', callback: (player: SupportedPlayerTypes) => {
if (player.isPlaying()) {
player.pause();
} else {
player.play();
}
}
};
toggle_mute = <KeyToFunctionBinding>{
keyBinding: 'm', callback: (player: SupportedPlayerTypes) => {
if (player.isMuted()) {
player.unmute();
} else {
player.mute();
}
}
};
enter_fullscreen = <KeyToFunctionBinding>{
keyBinding: 'f', callback: (player: SupportedPlayerTypes) => {
if (player.getViewMode() !== bitmovin.player.ViewMode.Fullscreen) {
player.setViewMode(bitmovin.player.ViewMode.Fullscreen);
}
}
};
exit_fullscreen = <KeyToFunctionBinding>{
keyBinding: 'esc', callback: (player: SupportedPlayerTypes) => {
if (player.getViewMode() === bitmovin.player.ViewMode.Fullscreen) {
player.setViewMode(bitmovin.player.ViewMode.Inline);
}
}
};
seek_plus5_sec = <KeyToFunctionBinding>{
keyBinding: 'right', callback: (player: SupportedPlayerTypes) => {
player.seek(Math.min(player.getDuration(), player.getCurrentTime() + 5));
}
};
seek_plus10_sec = <KeyToFunctionBinding>{
keyBinding: 'ctrl+right / command+right', callback: (player: SupportedPlayerTypes) => {
player.seek(Math.min(player.getDuration(), player.getCurrentTime() + 10));
}
};
seek_minus5_sec = <KeyToFunctionBinding>{
keyBinding: 'left', callback: (player: SupportedPlayerTypes) => {
player.seek(Math.max(0, player.getCurrentTime() - 5));
}
};
seek_minus10_sec = <KeyToFunctionBinding>{
keyBinding: 'ctrl+left / command+left', callback: (player: SupportedPlayerTypes) => {
player.seek(Math.max(0, player.getCurrentTime() - 10));
}
};
volume_plus5 = <KeyToFunctionBinding> {
keyBinding: 'up', callback: (player: SupportedPlayerTypes) => {
player.setVolume(Math.min(100, player.getVolume() + 5));
}
};
volume_plus10 = <KeyToFunctionBinding> {
keyBinding: 'ctrl+up / command+up', callback: (player: SupportedPlayerTypes) => {
player.setVolume(Math.min(100, player.getVolume() + 10));
}
};
volume_minus5 = <KeyToFunctionBinding> {
keyBinding: 'down', callback: (player: SupportedPlayerTypes) => {
player.setVolume(Math.max(0, player.getVolume() - 5));
}
};
volume_minus10 = <KeyToFunctionBinding> {
keyBinding: 'ctrl+down / command+down', callback: (player: SupportedPlayerTypes) => {
player.setVolume(Math.max(0, player.getVolume() - 10));
}
};
getAllBindings(): KeyToFunctionBinding[] {
let retVal = [];
// collect all objects of this keymap
// Do not use a static approach here as everything can be overwritten / extended
for (let attr in this) {
if (typeof this[attr] === 'object') {
retVal.push(this[attr]);
}
}
return retVal;
}
getAllBindingsForKey(keyRepresentation: string): KeyToFunctionBinding[] {
let retVal = [];
let allBindings = this.getAllBindings();
// split the key command by + and check all parts seperatly so we have the same behavior with ctrl+alt as with alt+ctrl
let allNeededKeys = keyRepresentation.split(KeyboardEventMapper.KeyCommandSeparator);
allBindings.forEach((element: KeyToFunctionBinding) => {
element.keyBinding.split(KeyboardEventMapper.KeyBindingSeparator).forEach((singleBinding: string) => {
let containsAllParts = true;
// make sure that the same amount of keys is needed and then make sure that all keys are contained
let singleBindingParts = singleBinding.split(KeyboardEventMapper.KeyCommandSeparator);
if (allNeededKeys.length === singleBindingParts.length) {
allNeededKeys.forEach((keyCommandPart: string) => {
if (singleBindingParts.indexOf(keyCommandPart) < 0) {
containsAllParts = false;
}
});
if (containsAllParts) {
retVal.push(element);
}
}
})
});
return retVal;
}
}
/**
* Definition of a Keyboard -> PlayerControl binding.
* the player Method signals what should happen on the player, but the actual behaviour is completely controlled by
* the callback.
* If you wish to overwrite the default behavior you can overwrite the default listeners in the config. Any Binding defined
* there will overwrite the default config.
*/
interface KeyToFunctionBinding {
/**
* the actual functionality of the binding, gets the player as a parameter
*/
callback: Function;
/**
* The keycode to listen to. Multiple bindings can listen to the same key.
*/
keyBinding: string;
}
/**
* Definition of all player functions which are bound to a keystroke by default.
* It is possible to configure any number of unknown KeyToFunctionBindings via the player configuration
*/
interface PlayerKeyMap {
toggle_play: KeyToFunctionBinding;
toggle_mute: KeyToFunctionBinding;
enter_fullscreen: KeyToFunctionBinding;
exit_fullscreen: KeyToFunctionBinding;
seek_plus5_sec: KeyToFunctionBinding;
seek_plus10_sec: KeyToFunctionBinding;
seek_minus5_sec: KeyToFunctionBinding;
seek_minus10_sec: KeyToFunctionBinding;
volume_plus5: KeyToFunctionBinding;
volume_plus10: KeyToFunctionBinding;
volume_minus5: KeyToFunctionBinding;
volume_minus10: KeyToFunctionBinding;
/**
* Retrieves a collection of all bindings of this KeyMap
*/
getAllBindings(): KeyToFunctionBinding[];
/**
* Filters all bindings of this KeyMap to find the bindings which have a matching keyBinding
* @param keyRepresentation the string representation of the desired keyStroke
*/
getAllBindingsForKey(keyRepresentation: string): KeyToFunctionBinding[];
}
/**
* All Player types which are supported by this class
*/
type SupportedPlayerTypes = any;
/**
* Class to control a given player instance via the keyboard
*/
class PlayerKeyboardControl {
private keyMap: PlayerKeyMap;
private isEnabled: boolean;
private player: SupportedPlayerTypes;
private shouldPreventScrolling: boolean;
constructor(wrappedPlayer: SupportedPlayerTypes, preventPageScroll = true, config?: any) {
this.player = wrappedPlayer;
this.shouldPreventScrolling = preventPageScroll;
let paramKeyMap = {};
if (config) {
paramKeyMap = config;
}
this.keyMap = PlayerKeyboardControl.mergeConfigWithDefault(paramKeyMap);
// default to enabled
// this also registers the event listeners
this.enable(true);
// destroy this together with the player
this.player.on(bitmovin.player.PlayerEvent.Destroy, () => {
this.destroy();
});
}
public enable(shouldBeEnabled: boolean = true) {
this.isEnabled = shouldBeEnabled;
// depending if we are enabled register or remove the keyListener
// we cannot use the keypress event as that event does not work with modifiers
// only add the keyUp listener as we do not expect users holding buttons to control the player
if (this.isEnabled) {
// in order to stop the browser from scrolling we have to add an additional onKeyDown listener
// because the browser would scroll on that one already
if(this.shouldPreventScrolling) {
document.addEventListener('keydown', this.preventScrolling, false);
}
document.addEventListener('keyup', this.handleKeyEvent, false);
} else {
// document.addEventListener('keypress', this.handleKeyEvent, false);
document.removeEventListener('keydown', this.preventScrolling, false);
document.removeEventListener('keyup', this.handleKeyEvent, false);
}
}
public disable(shouldBeDisabled: boolean = true) {
this.enable(!shouldBeDisabled);
}
/**
* Use this method to prevent the browser from scrolling via keyboard
* @param preventScrolling true if keyboard scrolling should be prevented, false if nots
*/
public setPreventScrolling(preventScrolling: boolean): void {
this.shouldPreventScrolling = preventScrolling;
// set up or remove the listener if necessary
if (this.isEnabled) {
if (preventScrolling) {
document.addEventListener('keydown', this.preventScrolling, false);
} else {
document.removeEventListener('keydown', this.preventScrolling, false);
}
}
}
public destroy() {
// removes the listener
this.disable(true);
}
protected static mergeConfigWithDefault(paramKeyMap: object): PlayerKeyMap {
let retVal = new DefaultPlayerKeymap();
// allow overwrites to the default player keymap as well as new listeners
for (let attr in paramKeyMap) {
if (attr && paramKeyMap[attr]) {
let toCheck = paramKeyMap[attr];
// avoid wrong configs and check for elements being real keyListeners
if (toCheck['keyBinding'] && toCheck['callback']) {
retVal[attr] = paramKeyMap[attr];
} else {
console.log('Invalid Key Listener at params[' + attr + ']');
}
}
}
return retVal;
}
public preventScrolling = (event: KeyboardEvent) => {
const keyCode = event.which || event.keyCode;
// prevent scrolling with arrow keys, space, pageUp and pageDown
if (KeyboardEventMapper.isScrollKey(keyCode)) {
// maybe we should check here if we actually have a keybinding for the keyCode and only prevent
// the scrolling if we actually handle the event
event.preventDefault();
}
};
public handleKeyEvent = (event: KeyboardEvent) => {
if (this.isEnabled) {
let keyStringRepresentation = KeyboardEventMapper.convertKeyboardEventToString(event);
let bindings = this.keyMap.getAllBindingsForKey(keyStringRepresentation);
bindings.forEach((singleBinding: KeyToFunctionBinding) => {
singleBinding.callback(this.player);
});
}
};
}
/**
* Class to handle mappings from KeyboardEvent.keyCode to a string representation
*/
class KeyboardEventMapper {
public static KeyBindingSeparator = ' / ';
public static KeyCommandSeparator = '+';
/**
* keys which will represented as a modifier
*/
public static ModifyerKeys = {
16: 'shift',
17: 'ctrl',
18: 'alt',
20: 'capslock',
91: 'meta',
93: 'meta',
224: 'meta'
};
/**
* Special keys on the keyboard which are not modifiers
*/
public static ControlKeys = {
8: 'backspace',
9: 'tab',
13: 'enter',
19: 'pause',
27: 'esc',
32: 'space',
33: 'pageup',
34: 'pagedown',
35: 'end',
36: 'home',
37: 'left',
38: 'up',
39: 'right',
40: 'down',
44: 'print',
45: 'ins',
46: 'del',
145: 'scrolllock',
186: ';',
187: '=',
188: ',',
189: '-',
190: '.',
191: '/',
192: '`',
219: '[',
220: '\\',
221: ']',
222: '\''
};
/**
* Keys wich normally move the page
*/
public static ScrollingKeys = {
32: 'space',
33: 'pageup',
34: 'pagedown',
35: 'end',
36: 'home',
37: 'left',
38: 'up',
39: 'right',
40: 'down',
};
/**
* All number on the numblock an the keys surrounding it
*/
public static NumblockKeys = {
96: '0',
97: '1',
98: '2',
99: '3',
100: '4',
101: '5',
102: '6',
103: '7',
104: '8',
105: '9',
106: '*',
107: '+',
109: '-',
110: '.',
111: '/',
144: 'numlock'
};
/**
* F1 - F19
*/
public static F_Keys = {
112: 'F1',
113: 'F2',
114: 'F3',
115: 'F4',
116: 'F5',
117: 'F6',
118: 'F7',
119: 'F8',
120: 'F9',
121: 'F10',
122: 'F11',
123: 'F12',
124: 'F13',
125: 'F14',
126: 'F15',
127: 'F16',
128: 'F17',
129: 'F18',
130: 'F19',
};
/**
* Converts a Keyboard Event to something like shit+alt+g depending on the character code of the event and the modifiers
* @param event the event to be converted into a string
* @returns {string} the representation of the all keys which were pressed
*/
public static convertKeyboardEventToString(event: KeyboardEvent): string {
let retVal = '';
let needsConcat: boolean = false;
if (event.shiftKey) {
retVal += 'shift';
needsConcat = true;
}
if (event.altKey) {
if (needsConcat) {
retVal += KeyboardEventMapper.KeyCommandSeparator;
} else {
needsConcat = true;
}
retVal += 'alt'
}
if (event.ctrlKey || event.metaKey) {
if (needsConcat) {
retVal += KeyboardEventMapper.KeyCommandSeparator;
} else {
needsConcat = true;
}
retVal += 'ctrl'
}
let convertedCode = KeyboardEventMapper.convertKeyCodeToString(event);
if (convertedCode) {
if (needsConcat) {
retVal += KeyboardEventMapper.KeyCommandSeparator;
}
retVal += convertedCode;
} else {
console.log('No conversion for the code: ' + event.keyCode);
}
return retVal;
}
/**
* Tries to convert a given keyCode to a string representation of the key
* @param event the event which contains the keyCode
* @returns {string} the string representation of the keyCode (eg.: 'left', 'esc', 'space', 'a', '1' ...)
*/
public static convertKeyCodeToString(event: KeyboardEvent): string {
let code = event.which || event.keyCode;
let retVal: string;
if (KeyboardEventMapper.isModifierKey(code)) {
retVal = KeyboardEventMapper.ModifyerKeys[code];
}
else if (KeyboardEventMapper.isControlKey(code)) {
retVal = KeyboardEventMapper.ControlKeys[code];
}
else if (KeyboardEventMapper.isNumblockKey(code)) {
retVal = KeyboardEventMapper.NumblockKeys[code];
}
else if (KeyboardEventMapper.isFKey(code)) {
retVal = KeyboardEventMapper.F_Keys[code];
}
else {
// try and convert a unicode character
retVal = String.fromCharCode(code).toLowerCase();
}
return retVal;
}
public static isModifierKey(keyCode: number): boolean {
return KeyboardEventMapper.ModifyerKeys.hasOwnProperty('' + keyCode);
}
public static isControlKey(keyCode: number): boolean {
return KeyboardEventMapper.ControlKeys.hasOwnProperty('' + keyCode);
}
public static isNumblockKey(keyCode: number): boolean {
return KeyboardEventMapper.NumblockKeys.hasOwnProperty('' + keyCode);
}
public static isFKey(keyCode: number): boolean {
return KeyboardEventMapper.F_Keys.hasOwnProperty('' + keyCode);
}
public static isScrollKey(keyCode: number): boolean {
return KeyboardEventMapper.ScrollingKeys.hasOwnProperty('' + keyCode)
}
}