我正在使用 videoJs 将视频加载到管理员中 内置 react 平台。我在这里设置播放器:
else if (contentSource == 'arclight'){
var options = {
height:'216',
sources :[{
//src: this.state.video.url,
// type: 'video/mp4'
}]
}
var player = videojs('player',options,function onPlayerReady() {
videojs.log('Your player is ready!');
})
this.setState({
player:player,content: contentSource
});
}
我的视频显示在这个标签中:
<video id="player" class="player"
className={`video-js vjs-default-skin vjs-big-play-centered
${styles.vjs}`}
controls preload= "auto" data-
setup='{"example_option":true}'>
<source src={this.state.video.url} type= "video/mp4" />
<p className="vjs-no-js">
To view this video please enable JavaScript, and consider
upgrading to a web browser that
<a href= "http://videojs.com/html5-video-support"
target="_blank"> supports HTML5 video </a>
</p>
</video>
最后,我试图获取以这种方法播放的视频的当前时间
handleCurrentButton(inputId, event){
event.preventDefault();
var timeMark =0;
if(this.state.content == 'vimeo'){
this.state.player.getCurrentTime().then(function(seconds){console.log(seconds)
timeMark=seconds;
VideosActions.updateVideoAttribute(inputId, timeMark);
}).catch(function(error){
});
}
else if(this.state.content == 'youtube') {
timeMark = Math.round(this.state.player.getCurrentTime());
VideosActions.updateVideoAttribute(inputId, timeMark);
}
else {
// timeMark = this.state.player.currentTime();
console.log(this.state.player.currentTime())
VideosActions.updateVideoAttribute(inputId, timeMark);
}
}
问题是 player.currentTime() 调用总是返回 0。Vimeo 和 Youtube 的其他两个 getCurrentTime 工作正常。这背后的原因是什么?我试图围绕这个问题提供足够的上下文,以便您能够弄清楚。 提前感谢您的帮助!
最佳答案
问题是 getCurrentTime() 返回一个 promise ,因此您需要访问 promise 解决时的时间值,作为对 Promise 的 .then()
函数的回调。
else {
this.state.player.currentTime().then(function(seconds){
console.log(seconds)
timeMark=seconds;
VideosActions.updateVideoAttribute(inputId, timeMark);
});
}
关于javascript - VideoJS currentTime() 总是返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43770071/