简体   繁体   中英

How to create a offset plugin for HTML5 video player

Am trying to create a offset plugin to play a portion of the video from full video in HTML5 video player, there is the plugin available for video.js videojs-offset plugin , now am trying to convert this plugin to work with HTML5 player alone

Source of videojs offset plugin

(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);

What is the way i need to try this could work with HTML5 video player alone ?

This seems like something you could easily do with a minimal amount of code. I'd suggest writing something like this:

 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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