javascript - 如何为 HTML5 视频播放器创建偏移量插件

标签 javascript jquery html video

我正在尝试创建一个偏移插件来在 HTML5 视频播放器中播放完整视频中的部分视频,该插件可用于 video.js videojs-offset plugin ,现在正在尝试将此插件转换为单独使用 HTML5 播放器

videojs offset插件来源

(function() {
    "use strict";
    var a = function(a) {
        var b;
        return this._offsetStart = a.start || 0, this._offsetEnd = a.end || 0, b = this.constructor, b.__super__ && b.__super__.__offsetInit || (b.__super__ = {
            __offsetInit: !0,
            duration: b.prototype.duration,
            currentTime: b.prototype.currentTime,
            bufferedPercent: b.prototype.bufferedPercent,
            remainingTime: b.prototype.remainingTime
        }, b.prototype.duration = function() {
            return this._offsetEnd > 0 ? this._offsetEnd - this._offsetStart : b.__super__.duration.apply(this, arguments) - this._offsetStart
        }, b.prototype.currentTime = function(a) {
            return void 0 !== a ? b.__super__.currentTime.call(this, a + this._offsetStart) - this._offsetStart : b.__super__.currentTime.apply(this, arguments) - this._offsetStart
        }, b.prototype.remainingTime = function() {
            var a = this.currentTime();
            return a < this._offsetStart && (a = 0), this.duration() - a
        }, b.prototype.startOffset = function() {
            return this._offsetStart
        }, b.prototype.endOffset = function() {
            return this._offsetEnd > 0 ? this._offsetEnd : this.duration()
        }), this.on("timeupdate", function() {
            var a = this.currentTime();
            0 > a && (this.currentTime(0), this.play()), this._offsetEnd > 0 && a > this._offsetEnd - this._offsetStart && (this.currentTime(this._offsetEnd - this._offsetStart), this.pause())
        }), this
    };
    a.VERSION = "0.0.0", window.vjsoffset = a, window.videojs.plugin("offset", a)
}).call(this);

我需要尝试什么方法才能单独使用 HTML5 视频播放器?

最佳答案

这似乎是您可以使用最少的代码轻松完成的事情。我建议写这样的东西:

function videoSegment(elementOrQuery, start, stop){
  
  var element = typeof elementOrQuery === 'string'
    ? document.querySelector(elementOrQuery) : elementOrQuery;
  
  element.currentTime = start;
  
  // While playing, check if time is within bounds
  element.addEventListener('timeupdate', function(){
    if(element.currentTime < start) element.currentTime = start;
    if(element.currentTime > stop) element.pause();
  });
  // When starting to play make sure its within bounds
  element.addEventListener('play', function(){
    if(element.duration > stop || element.duration < start) element.currentTime = start;
  });
  // When pausing, check if theres a loop and repeat or reset to beginning
  element.addEventListener('pause', function(){
    if(element.currentTime > stop) element.currentTime = start;
    if(element.loop) element.play();
  });
  
}

videoSegment('video', 2, 4);
<video controls autoplay loop>
  <source src="http://techslides.com/demos/sample-videos/small.mp4" />
</video>

关于javascript - 如何为 HTML5 视频播放器创建偏移量插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32758059/

相关文章:

php - Twitter api 1.1 oauth 在搜索主题标签时失败

javascript - 如何使用 jQuery 获取具有水平滚动的元素中的第一个完全可见的元素?

php - WordPress "admin-ajax.php"404 错误

javascript - jquery滚动效果处理方法(scrollpoints, horizo​​ntal scroll "hijack")

html - 在 100% 高度 div 上垂直滚动

javascript - 如何在addEventListener中捕获两个相同的id但不同的类

javascript - iframe 调整高度自动不起作用

javascript - 未捕获的 TypeError * 不是函数

php - 无效参数和非法偏移量

html - 为 div 创建默认的 html5 按钮 css?