简体   繁体   English

HTML5 视频播放器防止搜索

[英]HTML5 video player prevent seeking

I'm creating a series of video tutorials and would like to prevent users from seeking forward and skipping sections.我正在创建一系列视频教程,并希望防止用户向前搜索和跳过部分。 I'll be using an HTML5 video player that will be used for desktop and iPad browsers.我将使用用于桌面和 iPad 浏览器的 HTML5 视频播放器。 Ideally, I'd like this to work on the iPhone as well, but I realize you have no control over the video on the phone since it uses the iPhone video player.理想情况下,我希望它也能在 iPhone 上运行,但我意识到您无法控制手机上的视频,因为它使用 iPhone 视频播放器。

How can I prevent users from seeking forward on an HTML5 video player?如何防止用户在 HTML5 视频播放器上向前搜索?

Another example for Video.js: Video.js 的另一个示例:

videojs('example_video_1').ready(function(){
  var player = this;
  var previousTime = 0;
  var currentTime = 0;
  var seekStart = null;

  player.on('timeupdate', function(){
    previousTime = currentTime;
    currentTime = player.currentTime();
  });

  player.on('seeking', function(){
    if(seekStart === null) {
      seekStart = previousTime;
    }
  });

  player.on('seeked', function() {
    if(currentTime > seekStart) {
      player.currentTime(seekStart);
    }
    seekStart = null;
  });
});

I only wanted to prevent seeking forward.我只是想阻止向前寻求。 I have more code in my system that allows them to pause and come back later.我的系统中有更多代码可以让他们暂停并稍后返回。 It records the current position and sets the video position on load.它记录当前位置并在加载时设置视频位置。

I'm using video.js.我正在使用 video.js。 I tried to use the timechange event, but it fires before seeking.我尝试使用 timechange 事件,但它在查找之前触发。 So, I resorted to using an interval to grab the current position every second.因此,我使用间隔来每秒抓取当前位置。 If the seeking position is greater than the current position, then set it back.如果搜索位置大于当前位置,则将其设置回原处。 Works great.效果很好。

var player = null;
var curpos = 0;
videojs('player').ready(function(){
  player = this;
});

player.on('seeking', function () {
  var ct = player.currentTime();
if(ct > curpos) {
  player.currentTime(curpos);
}
});

function getpos() {
  curpos = player.currentTime();
}
onesecond = setInterval(getpos, 1000);

If you really want to do this amazingly user-hostile thing, then you can make use of the controls attribute.如果你真的想做这种令人惊讶的用户敌对的事情,那么你可以使用控件属性。 You will have to implement any controls you do want to allow using JS.您将必须实现您希望允许使用 JS 的任何控件。

Of course, the user can always just view > source to get the URI of the video and download it.当然,用户总是可以通过查看 > source来获取视频的 URI 并下载它。

Thanks for your answer Rick.感谢您的回答瑞克。

I noticed that the user can drag and hold the seeker which allowed them to continue so I added where not seeking to the getPos function.HTML 5 and jQuery.我注意到用户可以拖动并按住搜索器,这允许他们继续,所以我添加了不寻求 getPos 函数的地方。HTML 5 和 jQuery。

 var video = document.getElementsByTagName('video')[0]; function allowMove() { $('input[id$="btnNext"]').removeAttr('disabled'); $('input[id$="videoFlag"]').val("1"); } function getpos() { if (!(video.seeking)) { curpos = video.currentTime; } console.log(curpos) } onesecond = setInterval('getpos()', 1000); function setPos() { var ct = video.currentTime; if (ct > curpos) { video.currentTime = curpos; } }

我同意@Bart Kiers 的观​​点,这不是一个好主意,但如果您必须这样做,我可以想到一种方法:隐藏控件并提供您自己的播放按钮,使用 JavaScript 启动视频。

Why not this way?为什么不这样?

var video = document.getElementById("myVideo");
var counter = 0;

video.on('timeupdate', function() {
  if (video[0].paused == false) {
    counter = video[0].currentTime;
  }
}

video.on('seeking', function(e) {
  if ( parseInt(counter, 10) != parseInt(video[0].currentTime, 10) ) {
    video[0].currentTime = counter;
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM