向html5视频和javascript添加字幕

ejk8hzay  于 2021-09-23  发布在  Java
关注(0)|答案(0)|浏览(369)

我有一个使用javascript作为横幅播放的视频。我需要添加字幕曲目,但仅仅将字幕代码添加到视频部分是行不通的。我尝试在视频代码中添加vtt标题代码,但它不起作用。我在视频标签中添加了(track label=“english”kind=“subtitles”srclang=“en”src=“/media/images/home2018/banner/bannervideo3.vtt”)默认值,但这不起作用。我认为js需要以某种方式更新,以包括字幕跟踪(在曲目代码周围添加括号,因为此处不允许使用括号。)
我的代码如下:

<video id="BannerVideo3" class="Banner" muted playsinline loop oncanplaythrough="playBannerVideo3()" controls autoplay muted playsinline>
</video>

<script>
function playBannerVideo3()
{
  var videoElement = document.getElementById('BannerVideo3');
  try 
  {
    videoElement.play();
  }
  catch(err) {}
}

var basePosterURL = '/media/Images/Home2018/Banner/BannerVideo3.jpg';
var baseVideoURL = '/media/Images/Home2018/Banner/BannerVideo3.mp4';

function getNameForCurrentWidth()
{
  var bodyWidth = document.body.clientWidth; // $('body').width();
  if (bodyWidth <= 767)
    return '_Phone';

  if (bodyWidth <= 990)
    return '_Tablet';

  return ''; // Desktop
}

var loadedWidthName;
function loadBannerVideo3(posterOnly)
{
  var newWidthName = getNameForCurrentWidth();
  if(newWidthName == loadedWidthName)
    return;

  loadedWidthName = newWidthName;
  var videoElement = document.getElementById('BannerVideo3');

  if (!videoElement.paused)
    videoElement.pause();

  videoElement.src = '';
  videoElement.poster = addToFilename(basePosterURL, loadedWidthName);

  if (posterOnly == true)
    return;

  setBannerVideoSrc(loadedWidthName);
}

function setBannerVideo3Src(newWidthName)
{
  // don't play video if in design or edit modes
  if( $('body.DesignMode').length == 1 ||  $('body.EditMode').length == 1)
    return;

  var videoElement = document.getElementById('BannerVideo3');
  videoElement.src = addToFilename(baseVideoURL, newWidthName);
  videoElement.load();
}

// load poster as soon as possible
$(function() // document.ready
{
  loadBannerVideo3(true);
});

  // load and play video after everything else loads
$(window).on('load',function()
{
  setBannerVideo3Src(loadedWidthName);
});

var resizeTimeout;
$(window).resize(function()
{ 
  clearTimeout(resizeTimeout);
  resizeTimeout = setTimeout(resizeEnd, 200);   
});
function resizeEnd()
{
  loadBannerVideo();
}
</script>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题