-
Notifications
You must be signed in to change notification settings - Fork 2
/
Player.js
425 lines (318 loc) · 15.2 KB
/
Player.js
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
"use strict";
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactFastCompare = _interopRequireDefault(require("react-fast-compare"));
var _props = require("./props");
var _utils = require("./utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var SEEK_ON_PLAY_EXPIRY = 5000;
var Player = /*#__PURE__*/function (_Component) {
_inherits(Player, _Component);
var _super = _createSuper(Player);
function Player() {
var _this;
_classCallCheck(this, Player);
for (var _len = arguments.length, _args = new Array(_len), _key = 0; _key < _len; _key++) {
_args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(_args));
_defineProperty(_assertThisInitialized(_this), "mounted", false);
_defineProperty(_assertThisInitialized(_this), "isReady", false);
_defineProperty(_assertThisInitialized(_this), "isPlaying", false);
_defineProperty(_assertThisInitialized(_this), "isLoading", true);
_defineProperty(_assertThisInitialized(_this), "loadOnReady", null);
_defineProperty(_assertThisInitialized(_this), "startOnPlay", true);
_defineProperty(_assertThisInitialized(_this), "seekOnPlay", null);
_defineProperty(_assertThisInitialized(_this), "onDurationCalled", false);
_defineProperty(_assertThisInitialized(_this), "handlePlayerMount", function (player) {
_this.player = player;
if (!_this.loaded?.[_this.props.url]) {
_this.player.load(_this.props.url);
_this.loaded = { [_this.props.url]: true };
}
_this.progress();
});
_defineProperty(_assertThisInitialized(_this), "getInternalPlayer", function (key) {
if (!_this.player) return null;
return _this.player[key];
});
_defineProperty(_assertThisInitialized(_this), "progress", function () {
if (_this.props.url && _this.player && _this.isReady) {
var playedSeconds = _this.getCurrentTime() || 0;
var loadedSeconds = _this.getSecondsLoaded();
var duration = _this.getDuration();
if (duration) {
var progress = {
playedSeconds: playedSeconds,
played: playedSeconds / duration
};
if (loadedSeconds !== null) {
progress.loadedSeconds = loadedSeconds;
progress.loaded = loadedSeconds / duration;
} // Only call onProgress if values have changed
if (progress.playedSeconds !== _this.prevPlayed || progress.loadedSeconds !== _this.prevLoaded) {
_this.props.onProgress(progress);
}
_this.prevPlayed = progress.playedSeconds;
_this.prevLoaded = progress.loadedSeconds;
}
}
_this.progressTimeout = setTimeout(_this.progress, _this.props.progressFrequency || _this.props.progressInterval);
});
_defineProperty(_assertThisInitialized(_this), "handleReady", function () {
if (!_this.mounted) return;
_this.isReady = true;
_this.isLoading = false;
var _this$props = _this.props,
onReady = _this$props.onReady,
playing = _this$props.playing,
volume = _this$props.volume,
muted = _this$props.muted;
onReady();
if (!muted && volume !== null) {
_this.player.setVolume(volume);
}
if (_this.loadOnReady) {
_this.player.load(_this.loadOnReady, true);
_this.loadOnReady = null;
} else if (playing) {
_this.player.play();
}
_this.handleDurationCheck();
});
_defineProperty(_assertThisInitialized(_this), "handlePlay", function () {
_this.isPlaying = true;
_this.isLoading = false;
var _this$props2 = _this.props,
onStart = _this$props2.onStart,
onPlay = _this$props2.onPlay,
playbackRate = _this$props2.playbackRate;
if (_this.startOnPlay) {
if (_this.player.setPlaybackRate && playbackRate !== 1) {
_this.player.setPlaybackRate(playbackRate);
}
onStart();
_this.startOnPlay = false;
}
onPlay();
if (_this.seekOnPlay) {
_this.seekTo(_this.seekOnPlay);
_this.seekOnPlay = null;
}
_this.handleDurationCheck();
});
_defineProperty(_assertThisInitialized(_this), "handlePause", function (e) {
_this.isPlaying = false;
if (!_this.isLoading) {
_this.props.onPause(e);
}
});
_defineProperty(_assertThisInitialized(_this), "handleEnded", function () {
var _this$props3 = _this.props,
activePlayer = _this$props3.activePlayer,
loop = _this$props3.loop,
onEnded = _this$props3.onEnded;
if (activePlayer.loopOnEnded && loop) {
_this.seekTo(0);
}
if (!loop) {
_this.isPlaying = false;
onEnded();
}
});
_defineProperty(_assertThisInitialized(_this), "handleError", function () {
var _this$props4;
_this.isLoading = false;
(_this$props4 = _this.props).onError.apply(_this$props4, arguments);
});
_defineProperty(_assertThisInitialized(_this), "handleDurationCheck", function () {
clearTimeout(_this.durationCheckTimeout);
var duration = _this.getDuration();
if (duration) {
if (!_this.onDurationCalled) {
_this.props.onDuration(duration);
_this.onDurationCalled = true;
}
} else {
_this.durationCheckTimeout = setTimeout(_this.handleDurationCheck, 100);
}
});
_defineProperty(_assertThisInitialized(_this), "handleLoaded", function () {
// Sometimes we know loading has stopped but onReady/onPlay are never called
// so this provides a way for players to avoid getting stuck
_this.isLoading = false;
});
return _this;
}
_createClass(Player, [{
key: "componentDidMount",
value: function componentDidMount() {
this.mounted = true;
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
clearTimeout(this.progressTimeout);
clearTimeout(this.durationCheckTimeout);
if (this.isReady && this.props.stopOnUnmount) {
this.player.stop();
if (this.player.disablePIP) {
this.player.disablePIP();
}
}
this.mounted = false;
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
var _this2 = this;
// If there isn’t a player available, don’t do anything
if (!this.player) {
return;
} // Invoke player methods based on changed props
var _this$props5 = this.props,
url = _this$props5.url,
playing = _this$props5.playing,
volume = _this$props5.volume,
muted = _this$props5.muted,
playbackRate = _this$props5.playbackRate,
pip = _this$props5.pip,
loop = _this$props5.loop,
activePlayer = _this$props5.activePlayer,
disableDeferredLoading = _this$props5.disableDeferredLoading;
if (!(0, _reactFastCompare["default"])(prevProps.url, url)) {
if (this.isLoading && !activePlayer.forceLoad && !disableDeferredLoading && !(0, _utils.isMediaStream)(url)) {
console.warn("ReactPlayer: the attempt to load ".concat(url, " is being deferred until the player has loaded"));
this.loadOnReady = url;
return;
}
this.isLoading = true;
this.startOnPlay = true;
this.onDurationCalled = false;
if (!this.loaded?.[url]) {
this.player.load(url, this.isReady)
this.loaded = { [url]: true }
}
}
if (!prevProps.playing && playing && !this.isPlaying) {
this.player.play();
}
if (prevProps.playing && !playing && this.isPlaying) {
this.player.pause();
}
if (!prevProps.pip && pip && this.player.enablePIP) {
this.player.enablePIP();
}
if (prevProps.pip && !pip && this.player.disablePIP) {
this.player.disablePIP();
}
if (prevProps.volume !== volume && volume !== null) {
this.player.setVolume(volume);
}
if (prevProps.muted !== muted) {
if (muted) {
this.player.mute();
} else {
this.player.unmute();
if (volume !== null) {
// Set volume next tick to fix a bug with DailyMotion
setTimeout(function () {
return _this2.player.setVolume(volume);
});
}
}
}
if (prevProps.playbackRate !== playbackRate && this.player.setPlaybackRate) {
this.player.setPlaybackRate(playbackRate);
}
if (prevProps.loop !== loop && this.player.setLoop) {
this.player.setLoop(loop);
}
}
}, {
key: "getDuration",
value: function getDuration() {
if (!this.isReady) return null;
return this.player.getDuration();
}
}, {
key: "getCurrentTime",
value: function getCurrentTime() {
if (!this.isReady) return null;
return this.player.getCurrentTime();
}
}, {
key: "getSecondsLoaded",
value: function getSecondsLoaded() {
if (!this.isReady) return null;
return this.player.getSecondsLoaded();
}
}, {
key: "seekTo",
value: function seekTo(amount, type) {
var _this3 = this;
// When seeking before player is ready, store value and seek later
if (!this.isReady) {
if (amount !== 0) {
this.seekOnPlay = amount;
setTimeout(function () {
_this3.seekOnPlay = null;
}, SEEK_ON_PLAY_EXPIRY);
}
return;
}
var isFraction = !type ? amount > 0 && amount < 1 : type === 'fraction';
if (isFraction) {
// Convert fraction to seconds based on duration
var duration = this.player.getDuration();
if (!duration) {
console.warn('ReactPlayer: could not seek using fraction – duration not yet available');
return;
}
this.player.seekTo(duration * amount);
return;
}
this.player.seekTo(amount);
}
}, {
key: "render",
value: function render() {
var Player = this.props.activePlayer;
if (!Player) {
return null;
}
return /*#__PURE__*/_react["default"].createElement(Player, _extends({}, this.props, {
onMount: this.handlePlayerMount,
onReady: this.handleReady,
onPlay: this.handlePlay,
onPause: this.handlePause,
onEnded: this.handleEnded,
onLoaded: this.handleLoaded,
onError: this.handleError
}));
}
}]);
return Player;
}(_react.Component);
exports["default"] = Player;
_defineProperty(Player, "displayName", 'Player');
_defineProperty(Player, "propTypes", _props.propTypes);
_defineProperty(Player, "defaultProps", _props.defaultProps);