-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathvolume_bar.js
142 lines (117 loc) · 3.55 KB
/
volume_bar.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
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.ui.VolumeBar');
goog.require('goog.asserts');
goog.require('shaka.ads.Utils');
goog.require('shaka.ui.Controls');
goog.require('shaka.ui.Locales');
goog.require('shaka.ui.Localization');
goog.require('shaka.ui.RangeElement');
/**
* @extends {shaka.ui.RangeElement}
* @final
* @export
*/
shaka.ui.VolumeBar = class extends shaka.ui.RangeElement {
/**
* @param {!HTMLElement} parent
* @param {!shaka.ui.Controls} controls
*/
constructor(parent, controls) {
super(parent, controls,
['shaka-volume-bar-container'], ['shaka-volume-bar']);
/** @private {!shaka.extern.UIConfiguration} */
this.config_ = this.controls.getConfig();
// We use a range of 100 to avoid problems with Firefox.
// See https://github.com/shaka-project/shaka-player/issues/3987
this.setRange(0, 100);
this.eventManager.listen(this.video,
'volumechange',
() => this.onPresentationVolumeChange_());
this.eventManager.listen(this.adManager,
shaka.ads.Utils.AD_VOLUME_CHANGED,
() => this.onAdVolumeChange_());
this.eventManager.listen(this.adManager,
shaka.ads.Utils.AD_MUTED,
() => this.onAdVolumeChange_());
this.eventManager.listen(this.adManager,
shaka.ads.Utils.AD_STOPPED,
() => this.onPresentationVolumeChange_());
this.eventManager.listen(this.localization,
shaka.ui.Localization.LOCALE_UPDATED,
() => this.updateAriaLabel_());
this.eventManager.listen(this.localization,
shaka.ui.Localization.LOCALE_CHANGED,
() => this.updateAriaLabel_());
// Initialize volume display and label.
this.onPresentationVolumeChange_();
this.updateAriaLabel_();
if (this.ad) {
// There was already an ad.
this.onChange();
}
}
/**
* Update the video element's state to match the input element's state.
* Called by the base class when the input element changes.
*
* @override
*/
onChange() {
if (this.ad && this.ad.isLinear()) {
this.ad.setVolume(this.getValue() / 100);
} else {
this.video.volume = this.getValue() / 100;
if (this.video.volume == 0) {
this.video.muted = true;
} else {
this.video.muted = false;
}
}
}
/** @private */
onPresentationVolumeChange_() {
if (this.video.muted) {
this.setValue(0);
} else {
this.setValue(this.video.volume * 100);
}
this.updateColors_();
}
/** @private */
onAdVolumeChange_() {
goog.asserts.assert(this.ad != null,
'This.ad should exist at this point!');
const volume = this.ad.getVolume();
this.setValue(volume * 100);
this.updateColors_();
}
/** @private */
updateColors_() {
const colors = this.config_.volumeBarColors;
const gradient = ['to right'];
gradient.push(colors.level + this.getValue() + '%');
gradient.push(colors.base + this.getValue() + '%');
gradient.push(colors.base + '100%');
this.container.style.background =
'linear-gradient(' + gradient.join(',') + ')';
}
/** @private */
updateAriaLabel_() {
this.bar.ariaLabel = this.localization.resolve(shaka.ui.Locales.Ids.VOLUME);
}
};
/**
* @implements {shaka.extern.IUIElement.Factory}
* @final
*/
shaka.ui.VolumeBar.Factory = class {
/** @override */
create(rootElement, controls) {
return new shaka.ui.VolumeBar(rootElement, controls);
}
};
shaka.ui.Controls.registerElement('volume', new shaka.ui.VolumeBar.Factory());