I receive h.264/aac encoded mp4 video files and I want to play them back. Some of my video files have multiple audio streams or tracks within them (eg an english stream and a spanish stream). I want to allow the user to switch between the two audio tracks. I've tried video.js, which will play the video and default audio stream just fine, but I don't see any way to change audio tracks within the UI, and videojs.audioTracks() always returns an empty array (I'm using Chrome and I only need to support Chrome).
var video = document.getElementById("video");
video.audioTracks; // <-- undefined
AND
let video = videojs('video');
let audioTrackList = video.audioTracks();
audioTrackList.tracks; // <-- [] empty array
Does anyone know of a solution, whether it be something I'm doing wrong or missing in video.js, a plugin/extension for video.js, or a different player altogether?
EDIT:
Here's a basic example html file:
<html>
<head>
<link href="https://vjs.zencdn.net/7.7.5/video-js.css" rel="stylesheet" />
<body>
<video
id="my-video"
class="video-js"
controls
preload="auto"
width="640"
height="264"
data-setup="{}"
>
<source src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" type="video/mp4" />
<p class="vjs-no-js">
To view this video please enable JavaScript, and consider upgrading to a
web browser that
<a href="https://videojs.com/html5-video-support/" target="_blank"
>supports HTML5 video</a
>
</p>
</video>
<script src="https://vjs.zencdn.net/7.7.5/video.js"></script>
<script>
var vid = document.getElementById("my-video");
vid.onloadeddata = function() {
// get the current players AudioTrackList object
var player = videojs('my-video')
let tracks = player.audioTracks();
alert(tracks.length);
for (let i = 0; i < tracks.length; i++) {
let track = tracks[i];
console.log(track);
alert(track.language);
}
};
</script>
</body>
</html>
And it only ever alerts "0". No subsequent alerts and console logs. Tested in Chrome, Firefox, and Edge.
UPDATE: I finally discovered that enabling the flag, "enable-experimental-web-platform-features" in chrome://flags (https://caniuse.com/#feat=audiotracks) lets this method work.
Here's some basic html code:
<html>
<head>
<link href="https://vjs.zencdn.net/7.7.5/video-js.css" rel="stylesheet"/>
<body>
<video
id="my-video"
class="video-js"
controls
preload="auto"
width="640"
height="264"
data-setup="{}">
<source src="http://clips.vorwaerts-gmbh.de/VfE_html5.mp4" type="video/mp4"/>
<p class="vjs-no-js">
To view this video please enable JavaScript, and consider upgrading to a
web browser that
<a href="https://videojs.com/html5-video-support/" target="_blank"
>supports HTML5 video</a
>
</p>
</video>
<button onclick="onClick()">Toggle Audio</button>
<script src="https://vjs.zencdn.net/7.7.5/video.js"></script>
<script>
var VIDEO_JS = videojs('my-video');
function onClick() {
let tracks = VIDEO_JS.audioTracks();
tracks.tracks_[0].enabled = !tracks.tracks_[0].enabled;
tracks.tracks_[1].enabled = !tracks.tracks_[1].enabled;
}
</script>
</body>
</html>
But toggling the audio back and forth doesn't work well. The audio instantly toggles, but the video freezes until it catches up or something, but everything gets out of sync.