本文介绍了为什么在Chrome中设置HTML5视频元素的currentTime重置时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在HTML5中设置视频的时间位置。时间应该设置如下:
I want to set the time position of a video in HTML5. The time should be set like this:
function settime(){
var video = document.getElementById("video");
console.log(video.currentTime); //----->output for example 15.3
video.currentTime = 10.0;
console.log(video.currentTime);//----->>output always 0
}
视频嵌入如下:
<button onclick="settime();">Set Time</button>
<div class="container">
<video id="video1" class="video-js vjs-default-skin" muted>
<source src="video.m4v" type="video/mp4" />
HTML5 Video is required for this example.
</video>
但由于某种原因,这总是在Chrome中将currentTime重置为0。
But for some reason, this always just resets currentTime to 0 in Chrome.
为什么设置currentTime时会重置时间?我怎样才能正确设置currentTime?
Why gets the time reset when setting currentTime? And how can I set currentTime correctly?
推荐答案
我终于找到了答案! Chrome要求您将currentTime设置为字符串,而不是数字。
I finally figured out an answer! Chrome requires you to set the currentTime as a String, and not as a number.
function settime() {
var video = document.getElementById("video1");
console.log(video.currentTime); // output 0
video.currentTime = 10.0;
console.log(video.currentTime); // output 0 for some chrome users or 10 for firefox and others
video.currentTime = "10.0";
console.log(video.currentTime); // output 10
}
settime();
<audio id="video1" src="https://sample-videos.com/audio/mp3/crowd-cheering.mp3" controls></audio>
<div>Click play to start the audio at 10 s.</div>
这篇关于为什么在Chrome中设置HTML5视频元素的currentTime重置时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!