Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the default track of an html5 video element?

My page contains this <video> tag with several subtitle <track>s. I would like to enable on the fly the track by clicking on the corresponding national flag using jquery:

  <div id="lang">
    <img src="mini/ita.png" class="it" alt="Italiano" title="Italiano" />
    <img src="mini/eng.png" class="en" alt="English"  title="English"  />        
  </div>


<video controls="controls" >

  <source src="webcast.webm" />
  <source src="webcast.mp4"  />
  <source src="webcast.ogv"  />

  <track kind="subtitles" src="./sub-en.vtt" srclang="en" />
  <track kind="subtitles" src="./sub-it.vtt" srclang="it" />

</video>

<script>
$('#lang img').click(function(){
    language=$(this).attr('class');

    $('video track').removeAttr('default');
    $('video track[srclang='+language+']').attr('default','default');
});
</script>

The "default" attribute is correctly inserted, but no subtitle is displaied. Manually inserting the default attribute works perfectly (on chrome and opera).

like image 384
leonard vertighel Avatar asked Nov 13 '22 17:11

leonard vertighel


1 Answers

<script>
$('#lang img').click(function(){
    var language=$(this).attr('class');

    $($('video').prop('textTracks')).prop('mode', function(){
        return this.language == language ? 'showing' : 'disabled';
    });
});
</script>
like image 60
alexander farkas Avatar answered Nov 15 '22 05:11

alexander farkas