javascript - VideoJS currentTime() 总是返回 0

标签 javascript reactjs video.js

我正在使用 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/

相关文章:

javascript - 用同一网址上的新图片刷新图片

reactjs - 路由未定义React路由器和Spring Boot服务器

javascript - 将自定义 Prop 添加到组件实例(不是通过父级)-react-data-grid

javascript - 简单的 react 过滤

javascript - 在 video.js 中扩展进度控制,重复进度条

javascript - "Object doesn' t 在IE中支持属性或方法'find'”

javascript - Html DOM 对象未列出属性?

javascript - 为什么IOS和videojs会多次调用拉流?

php - 使用 Javascript 执行流程并将更新/回调发送到网络服务器

ffmpeg - hls.js - 如何增加预加载的缓冲区大小