Turning off closed caption on HTML5 video by defau

2019-07-20 06:24发布

问题:

Here's what I have...

<video id="video" controls="controls" preload="metadata" poster="/video/promotional/transparent.png">
    <source src="/video/promotional/promotional.mp4" type="video/mp4" />
    <source src="/video/promotional/promotional.webm" type="video/webm" />
    <source src="/video/promotional/promotional.ogv" type="video/ogg" />
    <track src="/video/promotional/promotional.vtt" label="English Captions" kind="subtitles" srclang="en-us" />
</video>

<script type="text/javascript">
    $(document).ready(function() {
        var video = document.querySelector('#video');
        var.track = video.textTracks[0];
        track.mode = 'hidden';
    });
</script>

I was hoping this would turn off closed caption by default, but it doesn't seem to be the case. Any ideas?

回答1:

your answer was very close (I assume var.track = was a typo - fixing that works in Chrome and Safari on OSX and IE on PC for me). For re-use I like to split the textTracks out into a variable, but that's personal preference:

<script type="text/javascript">
    $(document).ready(function() {
    var video = document.querySelector('#video'); // get the video element
    var tracks = video.textTracks; // one for each track element
    var track = tracks[0]; // corresponds to the first track element
    track.mode = 'hidden';
});