forked from lopezs/ableplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrack.js
More file actions
246 lines (223 loc) · 7.56 KB
/
track.js
File metadata and controls
246 lines (223 loc) · 7.56 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
(function ($) {
// Loads files referenced in track elements, and performs appropriate setup.
// For example, captions and text descriptions.
// This will be called whenever the player is recreated.
AblePlayer.prototype.setupTracks = function() {
var deferred = new $.Deferred();
var promise = deferred.promise();
this.$tracks = this.$media.find('track');
this.captions = [];
this.captionLabels = [];
this.descriptions = [];
this.chapters = [];
this.meta = [];
var loadingPromises = [];
for (var ii = 0; ii < this.$tracks.length; ii++) {
var track = this.$tracks[ii];
var kind = track.getAttribute('kind');
var trackSrc = track.getAttribute('src');
var isDefaultTrack = track.getAttribute('default');
if (!trackSrc) {
// Nothing to load!
continue;
}
var loadingPromise = this.loadTextObject(trackSrc);
var thisObj = this;
loadingPromises.push(loadingPromise);
loadingPromise.then((function (track, kind) {
return function (trackSrc, trackText) {
var cues = thisObj.parseWebVTT(trackSrc, trackText).cues;
if (kind === 'captions' || kind === 'subtitles') {
thisObj.setupCaptions(track, cues);
}
else if (kind === 'descriptions') {
thisObj.setupDescriptions(track, cues);
}
else if (kind === 'chapters') {
thisObj.setupChapters(track, cues);
}
else if (kind === 'metadata') {
thisObj.setupMetadata(track, cues);
}
}
})(track, kind));
}
$.when.apply($, loadingPromises).then(function () {
deferred.resolve();
});
return promise;
};
AblePlayer.prototype.setupCaptions = function (track, cues) {
this.hasCaptions = true;
// srcLang should always be included with <track>, but HTML5 spec doesn't require it
// if not provided, assume track is the same language as the default player language
var trackLang = track.getAttribute('srclang') || this.lang;
var trackLabel = track.getAttribute('label') || this.getLanguageName(trackLang);
if (typeof track.getAttribute('default') == 'string') {
var isDefaultTrack = true;
// Now remove 'default' attribute from <track>
// Otherwise, some browsers will display the track
track.removeAttribute('default');
}
else {
var isDefaultTrack = false;
}
// caption cues from WebVTT are used to build a transcript for both audio and video
// but captions are currently only supported for video
if (this.mediaType === 'video') {
// create a div for displaying captions
// includes aria-hidden="true" because otherwise
// captions being added and removed causes sporadic changes to focus in JAWS
// (not a problem in NVDA or VoiceOver)
if (!this.$captionDiv) {
this.$captionDiv = $('<div>',{
'class': 'able-captions',
'aria-hidden': 'true'
});
this.$vidcapContainer.append(this.$captionDiv);
}
}
this.currentCaption = -1;
if (this.prefCaptions === 1) {
// Captions default to on.
this.captionsOn = true;
}
else {
this.captionsOn = false;
}
if (this.includeTranscript) {
// Remove the "Unknown" option from the select box.
if (this.$unknownTranscriptOption) {
this.$unknownTranscriptOption.remove();
this.$unknownTranscriptOption = null;
}
var option = $('<option></option>',{
value: trackLang,
lang: trackLang
}).text(trackLabel);
}
// alphabetize tracks by label
if (this.includeTranscript) {
var options = this.$transcriptLanguageSelect.find('option');
}
if (this.captions.length === 0) { // this is the first
this.captions.push({
'cues': cues,
'language': trackLang,
'label': trackLabel,
'def': isDefaultTrack
});
if (this.includeTranscript) {
if (isDefaultTrack) {
option.attr('selected', 'selected');
}
this.$transcriptLanguageSelect.append(option);
}
this.captionLabels.push(trackLabel);
}
else { // there are already tracks in the array
var inserted = false;
for (var i = 0; i < this.captions.length; i++) {
var capLabel = this.captionLabels[i];
if (trackLabel.toLowerCase() < this.captionLabels[i].toLowerCase()) {
// insert before track i
this.captions.splice(i,0,{
'cues': cues,
'language': trackLang,
'label': trackLabel,
'def': isDefaultTrack
});
if (this.includeTranscript) {
if (isDefaultTrack) {
option.attr('selected', 'selected');
}
option.insertBefore(options.eq(i));
}
this.captionLabels.splice(i,0,trackLabel);
inserted = true;
break;
}
}
if (!inserted) {
// just add track to the end
this.captions.push({
'cues': cues,
'language': trackLang,
'label': trackLabel,
'def': isDefaultTrack
});
if (this.includeTranscript) {
if (isDefaultTrack) {
option.attr('selected', 'selected');
}
this.$transcriptLanguageSelect.append(option);
}
this.captionLabels.push(trackLabel);
}
}
if (this.includeTranscript) {
if (this.$transcriptLanguageSelect.find('option').length > 1) {
// More than one option now, so enable the select.
this.$transcriptLanguageSelect.prop('disabled', false);
}
}
};
AblePlayer.prototype.setupDescriptions = function (track, cues) {
var trackLang = track.getAttribute('srclang');
// descriptions are off unless determined to be available & preferred
this.descOn = false;
// prepare closed description, even if user doesn't prefer it
// this way it's available if needed
this.hasClosedDesc = true;
// Display the description div.
//this.$descDiv.show();
this.currentDescription = -1;
if ((this.prefDesc === 1) && (this.prefClosedDesc === 1)) {
this.descOn = true;
}
this.descriptions.push({
cues: cues,
language: trackLang
});
};
AblePlayer.prototype.setupChapters = function (track, cues) {
// NOTE: WebVTT supports nested timestamps (to form an outline)
// This is not currently supported.
this.hasChapters = true;
this.chapters = cues;
};
AblePlayer.prototype.setupMetadata = function(track, cues) {
// NOTE: Metadata is currently only supported if data-meta-div is provided
// The player does not display metadata internally
if (this.metaDiv) {
if ($('#' + this.metaDiv)) {
// container exists
this.$metaDiv = $('#' + this.metaDiv);
this.hasMeta = true;
}
}
this.meta = cues;
}
AblePlayer.prototype.loadTextObject = function(src) {
var deferred = new $.Deferred();
var promise = deferred.promise();
var thisObj = this;
// create a temp div for holding data
var $tempDiv = $('<div>',{
style: 'display:none'
});
$tempDiv.load(src, function (trackText, status, req) {
if (status === 'error') {
if (thisObj.debug) {
console.log ('error reading file ' + src + ': ' + status);
}
deferred.fail();
}
else {
deferred.resolve(src, trackText);
}
$tempDiv.remove();
});
return promise;
};
})(jQuery);