react-player
Version:
A react component for playing a variety of URLs, including file paths, YouTube, Facebook, SoundCloud, Streamable, Vidme, Vimeo and Wistia
189 lines (160 loc) • 6.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _Base2 = require('./Base');
var _Base3 = _interopRequireDefault(_Base2);
var _utils = require('../utils');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var SDK_URL = '//player.twitch.tv/js/embed/v1.js';
var SDK_GLOBAL = 'Twitch';
var MATCH_VIDEO_URL = /^(?:https?:\/\/)?(?:www\.)twitch\.tv\/videos\/(\d+)($|\?)/;
var MATCH_CHANNEL_URL = /^(?:https?:\/\/)?(?:www\.)twitch\.tv\/([a-z0-9_]+)($|\?)/;
var PLAYER_ID_PREFIX = 'twitch-player-';
var YouTube = function (_Base) {
_inherits(YouTube, _Base);
function YouTube() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, YouTube);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = YouTube.__proto__ || Object.getPrototypeOf(YouTube)).call.apply(_ref, [this].concat(args))), _this), _this.playerID = PLAYER_ID_PREFIX + (0, _utils.randomString)(), _this.onEnded = function () {
var _this$props = _this.props,
loop = _this$props.loop,
onEnded = _this$props.onEnded;
if (loop) {
_this.seekTo(0);
}
onEnded();
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(YouTube, [{
key: 'load',
value: function load(url) {
var _this2 = this;
var _props = this.props,
playsinline = _props.playsinline,
onError = _props.onError;
var isChannel = MATCH_CHANNEL_URL.test(url);
var id = isChannel ? url.match(MATCH_CHANNEL_URL)[1] : url.match(MATCH_VIDEO_URL)[1];
if (this.isReady) {
if (isChannel) {
this.player.setChannel(id);
} else {
this.player.setVideo('v' + id);
}
return;
}
if (this.loadingSDK) {
this.loadOnReady = url;
return;
}
this.loadingSDK = true;
(0, _utils.getSDK)(SDK_URL, SDK_GLOBAL).then(function (Twitch) {
_this2.player = new Twitch.Player(_this2.playerID, {
video: isChannel ? '' : id,
channel: isChannel ? id : '',
height: '100%',
width: '100%',
playsinline: playsinline
});
var _Twitch$Player = Twitch.Player,
READY = _Twitch$Player.READY,
PLAY = _Twitch$Player.PLAY,
PAUSE = _Twitch$Player.PAUSE,
ENDED = _Twitch$Player.ENDED;
_this2.player.addEventListener(READY, _this2.onReady);
_this2.player.addEventListener(PLAY, _this2.onPlay);
_this2.player.addEventListener(PAUSE, _this2.props.onPause);
_this2.player.addEventListener(ENDED, _this2.onEnded);
}, onError);
}
}, {
key: 'call',
value: function call(method) {
var _player;
if (!this.isReady || !this.player || !this.player[method]) return;
for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
args[_key2 - 1] = arguments[_key2];
}
return (_player = this.player)[method].apply(_player, args);
}
}, {
key: 'play',
value: function play() {
this.call('play');
}
}, {
key: 'pause',
value: function pause() {
this.call('pause');
}
}, {
key: 'stop',
value: function stop() {
this.call('pause');
}
}, {
key: 'seekTo',
value: function seekTo(amount) {
var seconds = _get(YouTube.prototype.__proto__ || Object.getPrototypeOf(YouTube.prototype), 'seekTo', this).call(this, amount);
this.call('seek', seconds);
}
}, {
key: 'setVolume',
value: function setVolume(fraction) {
this.call('setVolume', fraction);
}
}, {
key: 'setPlaybackRate',
value: function setPlaybackRate(rate) {
return null;
}
}, {
key: 'getDuration',
value: function getDuration() {
return this.call('getDuration');
}
}, {
key: 'getFractionPlayed',
value: function getFractionPlayed() {
var time = this.call('getCurrentTime');
var duration = this.getDuration();
if (time && duration) {
return time / duration;
}
return null;
}
}, {
key: 'getFractionLoaded',
value: function getFractionLoaded() {
return null;
}
}, {
key: 'render',
value: function render() {
var style = {
width: '100%',
height: '100%'
};
return _react2['default'].createElement('div', { style: style, id: this.playerID });
}
}], [{
key: 'canPlay',
value: function canPlay(url) {
return MATCH_VIDEO_URL.test(url) || MATCH_CHANNEL_URL.test(url);
}
}]);
return YouTube;
}(_Base3['default']);
YouTube.displayName = 'Twitch';
exports['default'] = YouTube;