![](/img/trans.png)
[英]How to add delay or offset to sync webvtt subtitles in html5 native video player?
[英]How to create a offset plugin for HTML5 video player
我試圖創建一個偏移插件來播放HTML5視頻播放器中完整視頻的一部分視頻,有一個可用於video.js videojs-offset插件的插件 ,現在正試圖將該插件轉換為僅可與HTML5播放器一起使用
videojs偏移插件的來源
(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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.