我如何通过一个开始HTML5视频在一个特定的时间代码使用URL

How can I pass a start an HTML5 video at a specific time code using the URL?

本文关键字:一个 时间 代码 URL HTML5 何通过 开始 视频      更新时间:2023-09-26

我需要一个视频在一个特定的时间开始,我需要这个时间由写入页面URL的值来确定。

我有这个video元素:
<video id="vid1" width="auto" height="auto" controls autoplay>
  <source src="video1.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>

此函数成功将视频提示到50秒:

<script>
document.getElementById('vid1').addEventListener('loadedmetadata',   function() {
  this.currentTime = 50;
}, false);
</script>

假设我的URL是http://www.mywebaddress.com/video1.html?50

我想从URL中获取值"50"并将其传递给我的函数,以便我可以动态更改视频提示的时间。

我有这个函数从URL获取值:

<script>
function GetUrlValue(VarSearch){
    var TimeCode = window.location.search.substring(1);
}
</script>

我现在如何重写提示函数,以便它将值设置为URL中选择的任何值?

你可以试试:

<script>
var TimeCode;
function GetUrlValue(){
    TimeCode = parseInt(window.location.href.split("?")[1]);
}
</script>

然后:

GetUrlValue();
document.getElementById('vid1').addEventListener('loadedmetadata',   function() {
  this.currentTime = TimeCode;
}, false);

您可以将this.currentTime的调用更改为

document.getElementById('vid1').addEventListener('loadedmetadata',   function() {
  this.currentTime = window.location.search.substring(1);
}, false);

或者按照你的变量机制你可以写

var TimeCode = window.location.search.substring(1);
document.getElementById('vid1').addEventListener('loadedmetadata',   function() {
  this.currentTime = TimeCode;
}, false);