-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathvolume.js
More file actions
273 lines (227 loc) · 7.5 KB
/
volume.js
File metadata and controls
273 lines (227 loc) · 7.5 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
(function ($) {
AblePlayer.prototype.addVolumeSlider = function($div) {
// Prior to v4.4.64, we were using a custom-build vertical volunme slider
// Changed to input type="range" because it's standard and gaining more widespread support
// including screen reader support
// TODO: Improve presentation of vertical slider. That requires some CSS finesse.
var thisObj, volumeSliderId, volumeHelpId, volumePct, volumeLabel, volumeHeight;
thisObj = this;
// define a few variables
volumeSliderId = this.mediaId + '-volume-slider';
volumeHelpId = this.mediaId + '-volume-help';
this.$volumeSlider = $('<div>',{
'id': volumeSliderId,
'class': 'able-volume-slider',
'aria-hidden': 'true'
}).hide();
this.$volumeSliderTooltip = $('<div>',{
'class': 'able-tooltip',
'role': 'tooltip'
}).hide();
this.$volumeRange = $('<input>',{
'type': 'range',
'min': '0',
'max': '10',
'step': '1',
'orient': 'vertical', // non-standard, but required for Firefox
'aria-label': this.translate( 'volumeUpDown', 'Volume up down' ),
'value': this.volume
});
volumePct = parseInt(thisObj.volume) / 10 * 100;
this.$volumeHelp = $('<div>',{
'id': volumeHelpId,
'class': 'able-volume-help',
'aria-live': 'polite'
}).text(volumePct + '%');
volumeLabel = this.$volumeButton.attr( 'aria-label' );
this.$volumeButton.attr( 'aria-label', volumeLabel + ' ' + volumePct + '%');
this.$volumeSlider.append(this.$volumeSliderTooltip,this.$volumeRange,this.$volumeHelp);
volumeHeight = this.$volumeButton.parents( '.able-control-row' )[0];
this.$volumeSlider.css( 'bottom', volumeHeight.offsetHeight );
$div.append(this.$volumeSlider);
// add event listeners
this.$volumeRange.on('change',function (e) {
thisObj.handleVolumeChange($(this).val());
});
this.$volumeRange.on('input',function (e) {
thisObj.handleVolumeChange($(this).val());
});
this.$volumeRange.on('keydown',function (e) {
if (e.key === 'Escape' || e.key === 'Tab' || e.key === 'Enter') {
// close popup
if (thisObj.$volumeSlider.is(':visible')) {
thisObj.closingVolume = true; // stopgap
thisObj.hideVolumePopup();
} else {
if (!thisObj.closingVolume) {
thisObj.showVolumePopup();
}
}
} else {
return;
}
});
};
AblePlayer.prototype.refreshVolumeHelp = function(volume) {
// make adjustments based on current volume
var volumePct;
volumePct = (volume/10) * 100;
// Update help text
if (this.$volumeHelp) {
this.$volumeHelp.text(volumePct + '%');
}
// Update the default value of the volume slider input field
// This doesn't seem to be necessary; browsers remember the previous setting during a session
// but this is a fallback in case they don't
this.$volumeRange.attr('value',volume);
};
AblePlayer.prototype.refreshVolumeButton = function(volume) {
var volumeName, volumePct, volumeLabel;
volumeName = this.getVolumeName(volume);
volumePct = (volume/10) * 100;
volumeLabel = this.translate( 'volume', 'Volume' ) + ' ' + volumePct + '%';
this.getIcon( this.$volumeButton, 'volume-' + volumeName );
this.$volumeButton.attr( 'aria-label', volumeLabel );
};
AblePlayer.prototype.handleVolumeButtonClick = function() {
if (this.$volumeSlider.is(':visible')) {
this.hideVolumePopup();
} else {
this.showVolumePopup();
}
};
AblePlayer.prototype.handleVolumeKeystroke = function(volume) {
// keyboard shortcuts for changing volume
if (this.isMuted() && volume > 0) {
this.setMute(false);
} else if (volume === 0) {
this.setMute(true);
} else {
this.setVolume(volume); // this.volume will be updated after volumechange event fires (event.js)
this.refreshVolumeHelp(volume);
this.refreshVolumeButton(volume);
}
};
AblePlayer.prototype.handleVolumeChange = function(volume) {
// handle volume change using the volume input slider
if (this.isMuted() && volume > 0) {
this.setMute(false);
} else if (volume === 0) {
this.setMute(true);
} else {
this.setVolume(volume); // this.volume will be updated after volumechange event fires (event.js)
this.refreshVolumeHelp(volume);
this.refreshVolumeButton(volume);
}
};
AblePlayer.prototype.handleMute = function() {
if (this.isMuted()) {
this.setMute(false);
} else {
this.setMute(true);
}
};
AblePlayer.prototype.showVolumePopup = function() {
this.closePopups();
this.$tooltipDiv.hide();
this.$volumeSlider.show().attr('aria-hidden','false');
this.$volumeButton.attr('aria-expanded','true');
this.$volumeButton.focus(); // for screen reader expanded state to be read
this.waitThenFocus(this.$volumeRange);
};
AblePlayer.prototype.hideVolumePopup = function() {
var thisObj = this;
this.$volumeSlider.hide().attr('aria-hidden','true');
this.$volumeButton.attr('aria-expanded','false').focus();
// wait a second before resetting stopgap var
// otherwise the keypress used to close volume popup will trigger the volume button
setTimeout(function() {
thisObj.closingVolume = false;
}, 1000);
};
AblePlayer.prototype.isMuted = function () {
if (this.player === 'html5') {
return this.media.muted;
} else if (this.player === 'youtube') {
return this.youTubePlayer.isMuted();
}
};
AblePlayer.prototype.setMute = function(mute) {
// mute is either true (muting) or false (unmuting)
if (mute) {
// save current volume so it can be restored after unmute
this.lastVolume = this.volume;
this.volume = 0;
} else { // restore to previous volume
if (typeof this.lastVolume !== 'undefined') {
this.volume = this.lastVolume;
}
}
if (this.player === 'html5') {
this.media.muted = mute;
} else if (this.player === 'youtube') {
if (mute) {
this.youTubePlayer.mute();
} else {
this.youTubePlayer.unMute();
}
}
this.setVolume(this.volume);
this.refreshVolumeHelp(this.volume);
this.refreshVolumeButton(this.volume);
};
AblePlayer.prototype.setVolume = function (volume) {
// volume is 1 to 10
// convert as needed depending on player
var newVolume;
this.syncSignVideo( {'volume' : 0 } );
if (this.player === 'html5') {
// volume is 0 to 1
newVolume = volume / 10;
this.media.volume = newVolume;
} else if (this.player === 'youtube') {
// volume is 0 to 100
newVolume = volume * 10;
this.youTubePlayer.setVolume(newVolume);
this.volume = volume;
} else if (this.player === 'vimeo') {
// volume is 0 to 1
newVolume = volume / 10;
this.vimeoPlayer.setVolume(newVolume).then(function() {
// setVolume finished.
// successful completion also fires a 'volumechange' event (see event.js)
});
}
this.lastVolume = volume;
};
AblePlayer.prototype.getVolume = function (volume) {
// return volume using common audio control scale 1 to 10
if (this.player === 'html5') {
// uses 0 to 1 scale
return this.media.volume * 10;
} else if (this.player === 'youtube') {
// uses 0 to 100 scale
if (this.youTubePlayerReady) {
return this.youTubePlayer.getVolume() / 10;
}
}
if (this.player === 'vimeo') {
// uses 0 to 1 scale
// this.vimeoPlayer.getVolume() takes too long to resolve with a value
// Just use variable that's already been defined (should be the same value anyway)
return this.volume;
}
};
AblePlayer.prototype.getVolumeName = function (volume) {
// returns 'mute','soft','medium', or 'loud' depending on volume level
if (volume == 0) {
return 'mute';
} else if (volume == 10) {
return 'loud';
} else if (volume < 5) {
return 'soft';
} else {
return 'medium';
}
};
})(jQuery);