-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathplugin.js
More file actions
160 lines (138 loc) · 4.51 KB
/
Copy pathplugin.js
File metadata and controls
160 lines (138 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import videojs from 'video.js';
import {version as VERSION} from '../package.json';
// Default options for the plugin.
const defaults = {};
// Cross-compatibility for Video.js 5 and 6.
const registerPlugin = videojs.registerPlugin || videojs.plugin;
// const dom = videojs.dom || videojs;
/**
* Checks whether the clip should be ended.
*
* @function onPlayerTimeUpdate
*
*/
const onPlayerTimeUpdate = function() {
const curr = this.currentTime();
if (curr < 0) {
this.currentTime(0);
this.play();
}
if (this._offsetEnd > 0 && curr > (this._offsetEnd - this._offsetStart)) {
this.off('timeupdate', onPlayerTimeUpdate);
this.pause();
this.trigger('ended');
// Re-bind to timeupdate next time the video plays
this.one('play', () => {
this.on('timeupdate', onPlayerTimeUpdate);
});
if (!this._restartBeginning) {
this.currentTime(this._offsetEnd - this._offsetStart);
} else {
this.trigger('loadstart');
this.currentTime(0);
}
}
};
/**
* Function to invoke when the player is ready.
*
* This is a great place for your plugin to initialize itself. When this
* function is called, the player will have its DOM and child components
* in place.
*
* @function onPlayerReady
* @param {Player} player
* A Video.js player.
* @param {Object} [options={}]
* An object of options left to the plugin author to define.
*/
const onPlayerReady = (player, options) => {
player.one('play', () => {
player.on('timeupdate', onPlayerTimeUpdate);
});
};
/**
* A video.js plugin.
*
* In the plugin function, the value of `this` is a video.js `Player`
* instance. You cannot rely on the player being in a "ready" state here,
* depending on how the plugin is invoked. This may or may not be important
* to you; if not, remove the wait for "ready"!
*
* @function offset
* @param {Object} [options={}]
* An object of options left to the plugin author to define.
*/
const offset = function(options) {
options = options || {};
const Player = this.constructor;
this._offsetStart = parseFloat(options.start || '0');
this._offsetEnd = parseFloat(options.end || '0');
this._restartBeginning = options.restart_beginning || false;
this.currentTime(0);
this.getCache().initTime = 0;
if (!Player.__super__ || !Player.__super__.__offsetInit) {
Player.__super__ = {
__offsetInit: true,
duration: Player.prototype.duration,
currentTime: Player.prototype.currentTime,
bufferedPercent: Player.prototype.bufferedPercent,
remainingTime: Player.prototype.remainingTime,
buffered: Player.prototype.buffered
};
Player.prototype.duration = function() {
if (this._offsetEnd !== undefined && this._offsetStart !== undefined) {
if (this._offsetEnd > 0) {
return this._offsetEnd - this._offsetStart;
}
return Player.__super__.duration.apply(this, arguments) - this._offsetStart;
}
return Player.__super__.duration.apply(this, arguments);
};
Player.prototype.currentTime = function(seconds) {
if (seconds !== undefined) {
if (this._offsetStart !== undefined) {
return Player.__super__.currentTime
.call(this, seconds + this._offsetStart);
}
return Player.__super__.currentTime.call(this, seconds);
}
if (this._offsetStart !== undefined) {
return Player.__super__.currentTime
.apply(this) - this._offsetStart;
}
return Player.__super__.currentTime.apply(this);
};
Player.prototype.remainingTime = function() {
return this.duration() - this.currentTime();
};
Player.prototype.startOffset = function() {
return this._offsetStart;
};
Player.prototype.endOffset = function() {
if (this._offsetEnd > 0) {
return this._offsetEnd;
}
return this.duration();
};
Player.prototype.buffered = function() {
const buff = Player.__super__.buffered.call(this);
const ranges = [];
for (let i = 0; i < buff.length; i++) {
ranges[i] = [
Math.max(0, buff.start(i) - this._offsetStart),
Math.min(Math.max(0, buff.end(i) - this._offsetStart), this.duration())
];
}
return videojs.createTimeRanges(ranges);
};
}
this.ready(() => {
onPlayerReady(this, videojs.mergeOptions(defaults, options));
});
};
// Register the plugin with video.js.
registerPlugin('offset', offset);
// Include the version number.
offset.VERSION = VERSION;
export default offset;