【问题标题】:currentTime() in video.jsvideo.js 中的 currentTime()
【发布时间】:2023-03-12 11:25:01
【问题描述】:

以下是我的代码,它不起作用。任何帮助将不胜感激。

var myPlayer;

videojs("example_video_1").ready(function(){

                    myPlayer = this;
if(myPlayer.currentTime()>3)
{
    
    
    
        alert("STARTED");

});

});

【问题讨论】:

  • 对不起。你的问题是什么?您收到任何错误吗?
  • 警报未到来。我不知道这段代码有什么问题。
  • 我希望在视频播放完 3 秒后立即显示警报。
  • ready 事件只会在最初加载视频时发生并运行一次您的代码。那时的当前时间可能是0。不过,您应该能够使用timeupdate event 来响应播放达到特定时间。
  • 所以你能建议我为了运行代码而应该做的改变吗?

标签: video.js


【解决方案1】:

ready 事件仅在视频最初加载时发生一次。此时,当前时间很可能是0

console.log(myPlayer.currentTime()); // 0

要继续检查时间的变化,您应该可以使用timeupdate event

myPlayer = this;
myPlayer.on('timeupdate', function () {
    // ...
});

但请注意,此事件每秒会发生多次。因此,为避免向自己发送警报,您可能需要跟踪是否已经过了 3 秒。

var threshold = 4;
var thresholdReached = false;

myPlayer = this;
myPlayer.on('timeupdate', function () {
    if (myPlayer.currentTime() >= threshold && !thresholdReached) {
        thresholdReached = true;
        alert('Started');
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多