简体   繁体   中英

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

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

Do like this..

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM