leonard vertighel
leonard vertighel

Reputation: 1068

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).

Upvotes: 5

Views: 4843

Answers (2)

alexander farkas
alexander farkas

Reputation: 14134

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

    $($('video').prop('textTracks')).prop('mode', function(){
        return this.language == language ? 'showing' : 'disabled';
    });
});
</script>

Upvotes: 2

arrest warrant
arrest warrant

Reputation: 354

$('#lang img').click(function(){
    language=your value;
    $('#yourID').attr('srclang', language); 
 });

Do like this..

Upvotes: 0

Related Questions