audio-track-selector.js
2.8 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
(function(videojs) {
var Component = videojs.getComponent('Component');
// -----------------
// AudioTrackMenuItem
// -----------------
//
var MenuItem = videojs.getComponent('MenuItem');
var AudioTrackMenuItem = videojs.extend(MenuItem, {
constructor: function(player, options) {
var track = options.track;
var tracks = player.audioTracks();
options.label = track.label || track.language || 'Unknown';
options.selected = track.enabled;
MenuItem.call(this, player, options);
this.track = track;
if (tracks) {
var changeHandler = videojs.bind(this, this.handleTracksChange);
tracks.addEventListener('change', changeHandler);
this.on('dispose', function() {
tracks.removeEventListener('change', changeHandler);
});
}
},
handleClick: function(event) {
var kind = this.track.kind;
var tracks = this.player_.audioTracks();
MenuItem.prototype.handleClick.call(this, event);
if (!tracks) return;
for (var i = 0; i < tracks.length; i++) {
var track = tracks[i];
if (track === this.track) {
track.enabled = true;
}
}
},
handleTracksChange: function(event) {
this.selected(this.track.enabled);
}
});
Component.registerComponent('AudioTrackMenuItem', AudioTrackMenuItem);
// -----------------
// AudioTrackButton
// -----------------
//
var MenuButton = videojs.getComponent('MenuButton');
var AudioTrackButton = videojs.extend(MenuButton, {
constructor: function(player, options) {
MenuButton.call(this, player, options);
this.el_.setAttribute('aria-label','Audio Menu');
var tracks = this.player_.audioTracks();
if (this.items.length <= 1) {
this.hide();
}
if (!tracks) {
return;
}
var updateHandler = videojs.bind(this, this.update);
tracks.addEventListener('removetrack', updateHandler);
tracks.addEventListener('addtrack', updateHandler);
this.player_.on('dispose', function() {
tracks.removeEventListener('removetrack', updateHandler);
tracks.removeEventListener('addtrack', updateHandler);
});
},
buildCSSClass() {
return 'vjs-subtitles-button ' + MenuButton.prototype.buildCSSClass.call(this);
},
createItems: function(items) {
items = items || [];
var tracks = this.player_.audioTracks();
if (!tracks) {
return items;
}
for (var i = 0; i < tracks.length; i++) {
var track = tracks[i];
items.push(new AudioTrackMenuItem(this.player_, {
'selectable': true,
'track': track
}));
}
return items;
}
});
Component.registerComponent('AudioTrackButton', AudioTrackButton);
})(window.videojs);