forked from boronia/ableplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdescription.js
More file actions
360 lines (335 loc) · 12.5 KB
/
description.js
File metadata and controls
360 lines (335 loc) · 12.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
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
(function ($) {
AblePlayer.prototype.initDescription = function() {
// set default mode for delivering description (open vs closed)
// based on availability and user preference
// called when player is being built, or when a user
// toggles the Description button or changes a description-related preference
// In the latter two scendarios, this.refreshingDesc == true via control.js > handleDescriptionToggle()
// The following variables are applicable to delivery of description:
// prefDesc == 1 if user wants description (i.e., Description button is on); else 0
// prefDescFormat == either 'video' or 'text' (as of v4.0.10, prefDescFormat is always 'video')
// prefDescPause == 1 to pause video when description starts; else 0
// prefVisibleDesc == 1 to visibly show text-based description area; else 0
// hasOpenDesc == true if a described version of video is available via data-desc-src attribute
// hasClosedDesc == true if a description text track is available
// this.useDescFormat == either 'video' or 'text'; the format ultimately delivered
// descOn == true if description of either type is on
// exposeTextDescriptions == true if text description is to be announced audibly; otherwise false
var thisObj = this;
if (this.refreshingDesc) {
this.prevDescFormat = this.useDescFormat;
}
else {
// this is the initial build
// first, check to see if there's an open-described version of this video
// checks only the first source since if a described version is provided,
// it must be provided for all sources
this.descFile = this.$sources.first().attr('data-desc-src');
if (typeof this.descFile !== 'undefined') {
this.hasOpenDesc = true;
}
else {
// there's no open-described version via data-desc-src,
// but what about data-youtube-desc-src or data-vimeo-desc-src?
if (this.youTubeDescId || this.vimeoDescId) {
this.hasOpenDesc = true;
}
else { // there are no open-described versions from any source
this.hasOpenDesc = false;
}
}
}
// update this.useDescFormat based on media availability & user preferences
if (this.prefDesc) {
if (this.hasOpenDesc && this.hasClosedDesc) {
// both formats are available. Always use 'video'
this.useDescFormat = this.prefDescFormat;
this.descOn = true;
// Do not pause during descriptions when playing described video
this.prefDescPause = false;
}
else if (this.hasOpenDesc) {
this.useDescFormat = 'video';
this.descOn = true;
}
else if (this.hasClosedDesc) {
this.useDescFormat = 'text';
this.descOn = true;
}
}
else { // description button is off
this.useDescFormat = false;
this.descOn = false;
}
if (this.useDescFormat === 'text') {
// check whether browser supports the Web Speech API
if (window.speechSynthesis) {
// It does!
this.synth = window.speechSynthesis;
this.descVoices = this.synth.getVoices();
// select the first voice that matches the track language
// available languages are identified with local suffixes (e.g., en-US)
// in case no matching voices are found, use the first voice in the voices array
this.descVoiceIndex = 0;
for (var i=0; i<this.descVoices.length; i++) {
if (this.captionLang.length === 2) {
// match only the first 2 characters of the lang code
if (this.descVoices[i].lang.substr(0,2).toLowerCase() === this.captionLang.toLowerCase()) {
this.descVoiceIndex = i;
break;
}
}
else {
// match the entire lang code
if (this.descVoices[i].lang.toLowerCase() === this.captionLang.toLowerCase()) {
this.descVoiceIndex = i;
break;
}
}
}
}
}
if (this.descOn) {
if (this.useDescFormat === 'video') {
if (!this.usingAudioDescription()) {
// switched from non-described to described version
this.swapDescription();
}
}
if (this.hasClosedDesc) {
if (this.prefVisibleDesc) {
// make description text visible
// New in v4.0.10: Do this regardless of useDescFormat
this.$descDiv.show();
this.$descDiv.removeClass('able-clipped');
}
else {
// keep it visible to screen readers, but hide it visibly
this.$descDiv.addClass('able-clipped');
}
if (!this.swappingSrc) {
this.showDescription(this.elapsed);
}
}
}
else { // description is off.
if (this.prevDescFormat === 'video') { // user was previously using description via video
if (this.usingAudioDescription()) {
this.swapDescription();
}
}
else if (this.prevDescFormat === 'text') { // user was previously using text description
// hide description div from everyone, including screen reader users
this.$descDiv.hide();
this.$descDiv.removeClass('able-clipped');
}
}
this.refreshingDesc = false;
};
// Returns true if currently using audio description, false otherwise.
AblePlayer.prototype.usingAudioDescription = function () {
if (this.player === 'youtube') {
return (this.activeYouTubeId === this.youTubeDescId);
}
else if (this.player === 'vimeo') {
return (this.activeVimeoId === this.vimeoDescId);
}
else {
return (this.$sources.first().attr('data-desc-src') === this.$sources.first().attr('src'));
}
};
AblePlayer.prototype.swapDescription = function() {
// swap described and non-described source media, depending on which is playing
// this function is only called in two circumstances:
// 1. Swapping to described version when initializing player (based on user prefs & availability)
// 2. User is toggling description
var thisObj, i, origSrc, descSrc, srcType, newSource;
thisObj = this;
// get current time, and start new video at the same time
// NOTE: There is some risk in resuming playback at the same start time
// since the described version might include extended audio description (with pauses)
// and might therefore be longer than the non-described version
// The benefits though would seem to outweigh this risk
this.swapTime = this.elapsed; // video will scrub to this time after loaded (see event.js)
if (this.descOn) {
// user has requested the described version
this.showAlert(this.tt.alertDescribedVersion);
}
else {
// user has requested the non-described version
this.showAlert(this.tt.alertNonDescribedVersion);
}
if (this.player === 'html5') {
if (this.usingAudioDescription()) {
// the described version is currently playing. Swap to non-described
for (i=0; i < this.$sources.length; i++) {
// for all <source> elements, replace src with data-orig-src
origSrc = this.$sources[i].getAttribute('data-orig-src');
srcType = this.$sources[i].getAttribute('type');
if (origSrc) {
this.$sources[i].setAttribute('src',origSrc);
}
}
// No need to check for this.initializing
// This function is only called during initialization
// if swapping from non-described to described
this.swappingSrc = true;
}
else {
// the non-described version is currently playing. Swap to described.
for (i=0; i < this.$sources.length; i++) {
// for all <source> elements, replace src with data-desc-src (if one exists)
// then store original source in a new data-orig-src attribute
origSrc = this.$sources[i].getAttribute('src');
descSrc = this.$sources[i].getAttribute('data-desc-src');
srcType = this.$sources[i].getAttribute('type');
if (descSrc) {
this.$sources[i].setAttribute('src',descSrc);
this.$sources[i].setAttribute('data-orig-src',origSrc);
}
}
this.swappingSrc = true;
}
// now reload the source file.
if (this.player === 'html5') {
this.media.load();
}
}
else if (this.player === 'youtube') {
if (this.usingAudioDescription()) {
// the described version is currently playing. Swap to non-described
this.activeYouTubeId = this.youTubeId;
this.showAlert(this.tt.alertNonDescribedVersion);
}
else {
// the non-described version is currently playing. Swap to described.
this.activeYouTubeId = this.youTubeDescId;
this.showAlert(this.tt.alertDescribedVersion);
}
if (typeof this.youTubePlayer !== 'undefined') {
// retrieve/setup captions for the new video from YouTube
this.setupAltCaptions().then(function() {
if (thisObj.playing) {
// loadVideoById() loads and immediately plays the new video at swapTime
thisObj.youTubePlayer.loadVideoById(thisObj.activeYouTubeId,thisObj.swapTime);
}
else {
// cueVideoById() loads the new video and seeks to swapTime, but does not play
thisObj.youTubePlayer.cueVideoById(thisObj.activeYouTubeId,thisObj.swapTime);
}
});
}
}
else if (this.player === 'vimeo') {
if (this.usingAudioDescription()) {
// the described version is currently playing. Swap to non-described
this.activeVimeoId = this.vimeoId;
this.showAlert(this.tt.alertNonDescribedVersion);
}
else {
// the non-described version is currently playing. Swap to described.
this.activeVimeoId = this.vimeoDescId;
this.showAlert(this.tt.alertDescribedVersion);
}
// load the new video source
this.vimeoPlayer.loadVideo(this.activeVimeoId).then(function() {
if (thisObj.playing) {
// video was playing when user requested an alternative version
// seek to swapTime and continue playback (playback happens automatically)
thisObj.vimeoPlayer.setCurrentTime(thisObj.swapTime);
}
else {
// Vimeo autostarts immediately after video loads
// The "Described" button should not trigger playback, so stop this before the user notices.
thisObj.vimeoPlayer.pause();
}
});
}
};
AblePlayer.prototype.showDescription = function(now) {
// there's a lot of redundancy between this function and showCaptions
// Trying to combine them ended up in a mess though. Keeping as is for now.
if (this.swappingSrc || !this.descOn) {
return;
}
var thisObj, i, cues, d, thisDescription, descText, msg;
thisObj = this;
var flattenComponentForDescription = function (component) {
var result = [];
if (component.type === 'string') {
result.push(component.value);
}
else {
for (var i = 0; i < component.children.length; i++) {
result.push(flattenComponentForDescription(component.children[i]));
}
}
return result.join('');
};
if (this.selectedDescriptions) {
cues = this.selectedDescriptions.cues;
}
else if (this.descriptions.length >= 1) {
cues = this.descriptions[0].cues;
}
else {
cues = [];
}
for (d = 0; d < cues.length; d++) {
if ((cues[d].start <= now) && (cues[d].end > now)) {
thisDescription = d;
break;
}
}
if (typeof thisDescription !== 'undefined') {
if (this.currentDescription !== thisDescription) {
// temporarily remove aria-live from $status in order to prevent description from being interrupted
this.$status.removeAttr('aria-live');
descText = flattenComponentForDescription(cues[thisDescription].components);
if (
this.exposeTextDescriptions &&
typeof this.synth !== 'undefined' &&
typeof this.descVoiceIndex !== 'undefined') {
// browser supports speech synthesis and a voice has been selected in initDescription()
// use the web speech API
msg = new SpeechSynthesisUtterance();
msg.voice = this.descVoices[this.descVoiceIndex]; // Note: some voices don't support altering params
msg.voiceURI = 'native';
msg.volume = 1; // 0 to 1
msg.rate = 1.5; // 0.1 to 10 (1 is normal human speech; 2 is fast but easily decipherable; anything above 2 is blazing fast)
msg.pitch = 1; //0 to 2
msg.text = descText;
msg.lang = this.captionLang;
msg.onend = function(e) {
// NOTE: e.elapsedTime might be useful
if (thisObj.pausedForDescription) {
thisObj.playMedia();
}
};
this.synth.speak(msg);
if (this.prefVisibleDesc) {
// write description to the screen for sighted users
// but remove ARIA attributes since it isn't intended to be read by screen readers
this.$descDiv.html(descText).removeAttr('aria-live aria-atomic');
}
}
else {
// browser does not support speech synthesis
// load the new description into the container div for screen readers to read
this.$descDiv.html(descText);
}
if (this.prefDescPause && this.exposeTextDescriptions) {
this.pauseMedia();
this.pausedForDescription = true;
}
this.currentDescription = thisDescription;
}
}
else {
this.$descDiv.html('');
this.currentDescription = -1;
// restore aria-live to $status
this.$status.attr('aria-live','polite');
}
};
})(jQuery);