简体   繁体   English

字幕轨道 HTML5 元素不要在视频中更新

[英]Subtitle Track HTML5 Element Don't Update in Video

I'm facing a problem, I need to change the subtitles track source dynamically from javascript.我遇到了一个问题,我需要从 javascript 动态更改字幕轨道源。

The current behaviour is that no change happens and the old subtitles keeps showing.当前的行为是没有发生任何变化,并且旧字幕继续显示。

This is the html:这是 html:

<video style="width: 50%;" class="m-5" id="vid" controls>
      <track src="oldPath.vtt" id="subtitleTrack" label="English" kind="subtitles" srclang="en" default />
</video>

This is the Javascript:这是 Javascript:

let subtitleTrack = document.getElementById("subtitleTrack");

function subtitleEdited(newTrackPath) {
  //....
  subtitleTrack.src = newTrackPath;
  //....
}

I need once the track source changed, the new captions should be loaded to the video directly.我需要一旦轨道源发生变化,新的字幕应该直接加载到视频中。

I tried loading the video again and it didn't work by adding video.load() after changing the track source.我尝试再次加载视频,但在更改轨道源后添加video.load()


Update更新

After further investigation the problem seems to be because of caching issues.经过进一步调查,问题似乎是由于缓存问题。 I need the new path to be the same old path ( the path has new updates localy ) But the browser takes his copy from the cache without updating it from the local files.我需要新路径是相同的旧路径(该路径在本地有新的更新)但是浏览器从缓存中获取他的副本而不从本地文件中更新它。 __ __

Second Update第二次更新

Thanks for the response of @Terry.感谢@Terry 的回复。

I tried adding versioning to the source, but it retrieves nothing.我尝试将版本控制添加到源中,但它什么也没得到。

Check the response size, The ?v=2 response it empty.检查响应大小, ?v=2响应为空。网络截图

PS The project is electron project.Anyways, I don't think that it can be part of the problem. PS该项目是electron项目。无论如何,我认为这不是问题的一部分。

As you have mentioned in your comments (and your updated question), that you are using the exact same track path.正如您在评论(和更新的问题)中提到的那样,您使用的是完全相同的轨道路径。 That will cause the browser to fetch from the cache instead since the file path has not changed: a good idea will be to simply add a cache-busting query string to the src attribute, so that the browser will ignore the cache:这将导致浏览器从缓存中获取,因为文件路径没有改变:一个好主意是简单地添加一个缓存破坏查询字符串到src属性,这样浏览器就会忽略缓存:

let subtitleTrack = document.getElementById("subtitleTrack");

function subtitleEdited(newTrackPath) {
  //....
  subtitleTrack.src = newTrackPath + '?t=' + new Date().getTime();
  //....
}

Of course if you're more comfortable with using template literals, this might be more readalbe:当然,如果您更习惯使用模板文字,这可能更具可读性:

subtitleTrack.src = `${newTrackPath}?t=${new Date().getTime()}`;

I miss understood the problem.我想念这个问题。

the problem was I was trying to access the subtitle file while other function was editing it.问题是我试图访问字幕文件,而其他 function 正在编辑它。 So I added a callback to prevent that.所以我添加了一个回调来防止这种情况。

And also by applying @Terry's answer to add that cache-busting query.并且还通过应用@Terry 的答案来添加缓存清除查询。

It is working now after applying the above changes.应用上述更改后,它现在正在工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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