index.html
3.44 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Multiple Alternative Audio Tracks - Example</title>
<link href="/node_modules/video.js/dist/video-js.css" rel="stylesheet">
</head>
<body>
<h1>Multiple Alternative Audio Tracks</h1>
<p>Check the source of this page and the console for detailed information on this example</p>
<video id="maat-player" class="video-js vjs-default-skin" controls>
<source src="https://s3.amazonaws.com/_bc_dml/example-content/bipbop-advanced/bipbop_16x9_variant.m3u8" type="application/x-mpegURL">
</video>
<div id="audioTrackChoice">
<select id="enabled-audio-track" name="enabled-audio-track">
</select>
</div>
<script src="/node_modules/video.js/dist/video.js"></script>
<script src="/dist/videojs-contrib-hls.js"></script>
<script>
(function(window, videojs) {
var player = window.player = videojs('maat-player');
var audioTrackList = player.audioTracks();
var audioTrackSelect = document.getElementById("enabled-audio-track");
// watch for a change on the select element
// then change the enabled audio track
// only one can be enabled at a time, but video.js will
// handle that for us, all we need to do is enable the new
// track
audioTrackSelect.addEventListener('change', function() {
var track = audioTrackList[this.selectedIndex];
console.log('User switched to track ' + track.label);
track.enabled = true;
});
// watch for changes that will be triggered by any change
// to enabled on any audio track. Manually or through the
// select element
audioTrackList.on('change', function() {
for (var i = 0; i < audioTrackList.length; i++) {
var track = audioTrackList[i];
if (track.enabled) {
console.log('A new ' + track.label + ' has been enabled!');
}
}
});
// will be fired twice in this example
audioTrackList.on('addtrack', function() {
console.log('a track has been added to the audio track list');
});
// will not be fired at all unless you call
// audioTrackList.removeTrack(trackObj)
// we typically will not need to do this unless we have to load
// another video for some reason
audioTrackList.on('removetrack', function() {
console.log('a track has been removed from the audio track list');
});
// getting all the possible audio tracks from the track list
// get all of thier properties
// add each track to the select on the page
// this is all filled out by HLS when it parses the m3u8
player.on('loadeddata', function() {
console.log('There are ' + audioTrackList.length + ' audio tracks');
for (var i = 0; i < audioTrackList.length; i++) {
var track = audioTrackList[i];
var option = document.createElement("option");
option.text = track.label;
if (track.enabled) {
option.selected = true;
}
audioTrackSelect.add(option, i);
console.log('Track ' + (i + 1));
['label', 'enabled', 'language', 'id', 'kind'].forEach(function(prop) {
console.log(" " + prop + ": " + track[prop]);
});
}
});
}(window, window.videojs));
</script>
</body>
</html>