UNPKG

react-player-custom

Version:

A React component for playing a variety of URLs, including file paths, YouTube, Facebook, Twitch, SoundCloud, Streamable, Vimeo, Wistia and DailyMotion

160 lines (133 loc) 6.1 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.Twitch = undefined; var _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; }; 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 _react = require('react'); var _react2 = _interopRequireDefault(_react); var _utils = require('../utils'); var _singlePlayer = require('../singlePlayer'); var _singlePlayer2 = _interopRequireDefault(_singlePlayer); 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 = 'https://player.twitch.tv/js/embed/v1.js'; var SDK_GLOBAL = 'Twitch'; var MATCH_VIDEO_URL = /(?:www\.|go\.)?twitch\.tv\/videos\/(\d+)($|\?)/; var MATCH_CHANNEL_URL = /(?:www\.|go\.)?twitch\.tv\/([a-z0-9_]+)($|\?)/; var PLAYER_ID_PREFIX = 'twitch-player-'; var Twitch = exports.Twitch = function (_Component) { _inherits(Twitch, _Component); function Twitch() { var _ref; var _temp, _this, _ret; _classCallCheck(this, Twitch); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Twitch.__proto__ || Object.getPrototypeOf(Twitch)).call.apply(_ref, [this].concat(args))), _this), _this.callPlayer = _utils.callPlayer, _this.playerID = PLAYER_ID_PREFIX + (0, _utils.randomString)(), _this.mute = function () { _this.callPlayer('setMuted', true); }, _this.unmute = function () { _this.callPlayer('setMuted', false); }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(Twitch, [{ key: 'load', value: function load(url, isReady) { var _this2 = this; var _props = this.props, playsinline = _props.playsinline, onError = _props.onError, config = _props.config; var isChannel = MATCH_CHANNEL_URL.test(url); var id = isChannel ? url.match(MATCH_CHANNEL_URL)[1] : url.match(MATCH_VIDEO_URL)[1]; if (isReady) { if (isChannel) { this.player.setChannel(id); } else { this.player.setVideo('v' + id); } return; } (0, _utils.getSDK)(SDK_URL, SDK_GLOBAL).then(function (Twitch) { _this2.player = new Twitch.Player(_this2.playerID, _extends({ video: isChannel ? '' : id, channel: isChannel ? id : '', height: '100%', width: '100%', playsinline: playsinline, autoplay: _this2.props.playing, muted: _this2.props.muted }, config.twitch.options)); var _Twitch$Player = Twitch.Player, READY = _Twitch$Player.READY, PLAYING = _Twitch$Player.PLAYING, PAUSE = _Twitch$Player.PAUSE, ENDED = _Twitch$Player.ENDED; _this2.player.addEventListener(READY, _this2.props.onReady); _this2.player.addEventListener(PLAYING, _this2.props.onPlay); _this2.player.addEventListener(PAUSE, _this2.props.onPause); _this2.player.addEventListener(ENDED, _this2.props.onEnded); }, onError); } }, { key: 'play', value: function play() { this.callPlayer('play'); } }, { key: 'pause', value: function pause() { this.callPlayer('pause'); } }, { key: 'stop', value: function stop() { this.callPlayer('pause'); } }, { key: 'seekTo', value: function seekTo(seconds) { this.callPlayer('seek', seconds); } }, { key: 'setVolume', value: function setVolume(fraction) { this.callPlayer('setVolume', fraction); } }, { key: 'getDuration', value: function getDuration() { return this.callPlayer('getDuration'); } }, { key: 'getCurrentTime', value: function getCurrentTime() { return this.callPlayer('getCurrentTime'); } }, { key: 'getSecondsLoaded', value: function getSecondsLoaded() { return null; } }, { key: 'render', value: function render() { var style = { width: '100%', height: '100%' }; return _react2['default'].createElement('div', { style: style, id: this.playerID }); } }]); return Twitch; }(_react.Component); Twitch.displayName = 'Twitch'; Twitch.canPlay = function (url) { return MATCH_VIDEO_URL.test(url) || MATCH_CHANNEL_URL.test(url); }; Twitch.loopOnEnded = true; exports['default'] = (0, _singlePlayer2['default'])(Twitch);