UNPKG

maestro-react-player

Version:

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

210 lines (178 loc) 7.14 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.UstreamVideo = undefined; 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://developers.ustream.tv/js/ustream-embedapi.min.js'; var SDK_GLOBAL = 'UstreamEmbed'; var MATCH_URL = /(ustream.tv\/recorded\/)([^#&?/]*)/; var PLAYER_ID_PREFIX = 'UstreamVideo-player-'; var UstreamVideo = exports.UstreamVideo = function (_Component) { _inherits(UstreamVideo, _Component); function UstreamVideo() { var _ref; var _temp, _this, _ret; _classCallCheck(this, UstreamVideo); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = UstreamVideo.__proto__ || Object.getPrototypeOf(UstreamVideo)).call.apply(_ref, [this].concat(args))), _this), _this.state = { ustreamSrc: null }, _this.playerID = PLAYER_ID_PREFIX + (0, _utils.randomString)(), _this.callPlayer = _utils.callPlayer, _this.mute = function () {}, _this.unmute = function () {}, _this.ref = function (container) { _this.container = container; }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(UstreamVideo, [{ key: 'parseId', value: function parseId(url) { var m = url.match(MATCH_URL); return m[2]; } }, { key: 'componentDidUpdate', value: function componentDidUpdate(prevProps) { // reset ustreamSrc on reload if (prevProps.url && prevProps.url !== this.props.url) { this.setState({ ustreamSrc: null }); } } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { // clear the interval below if (this.currentTimeInterval) { clearInterval(this.currentTimeInterval); } } // there's no events to update progress and duration, // so we're going to set an interval here. Also, duration // is zero or null for the first few seconds. Couldn't find // a deterministic event to let us know when we should grab the duration. }, { key: 'initInterval', value: function initInterval() { var _this2 = this; if (this.currentTimeInterval) { return; } this.currentTimeInterval = setInterval(function () { if (_this2.player) { _this2.player.getProperty('progress', function (progress) { _this2.player.currentTime = progress; }); _this2.player.getProperty('duration', function (duration) { _this2.player.duration = duration; }); } }, 500); } }, { key: 'load', value: function load() { var _this3 = this; var _props = this.props, onEnded = _props.onEnded, onError = _props.onError, onPause = _props.onPause, onPlay = _props.onPlay, onReady = _props.onReady, playing = _props.playing, url = _props.url; var videoId = this.parseId(url); this.setState({ ustreamSrc: 'https://www.ustream.tv/embed/recorded/' + videoId + '?html5ui=1&autoplay=' + playing + '&controls=false&showtitle=false' }); (0, _utils.getSDK)(SDK_URL, SDK_GLOBAL).then(function (UstreamEmbed) { if (!_this3.container) return; _this3.player = UstreamEmbed(_this3.playerID); _this3.player.addListener('playing', function (type, playing) { playing ? onPlay() : onPause(); }); _this3.player.addListener('ready', function () { _this3.initInterval(); onReady(); }); _this3.player.addListener('finished', onEnded); }, onError); } // todo // todo }, { key: 'play', value: function play() { this.callPlayer('callMethod', 'play'); } }, { key: 'pause', value: function pause() { this.callPlayer('callMethod', 'pause'); } }, { key: 'stop', value: function stop() { this.callPlayer('callMethod', 'stop'); } }, { key: 'seekTo', value: function seekTo(seconds) { this.callPlayer('callMethod', 'seek', seconds); } }, { key: 'setVolume', value: function setVolume(fraction) { this.callPlayer('callMethod', 'volume', fraction * 100); } }, { key: 'getDuration', value: function getDuration() { return this.player.duration; } }, { key: 'getCurrentTime', value: function getCurrentTime() { return this.player.currentTime; } }, { key: 'getSecondsLoaded', value: function getSecondsLoaded() { return null; } }, { key: 'render', value: function render() { var style = { width: '100%', height: '100%' }; var ustreamSrc = this.state.ustreamSrc; return ustreamSrc && _react2['default'].createElement('iframe', { id: this.playerID, ref: this.ref, src: ustreamSrc, frameBorder: '0', scrolling: 'no', style: style, allowFullScreen: true }); } }]); return UstreamVideo; }(_react.Component); UstreamVideo.displayName = 'UstreamVideo'; UstreamVideo.canPlay = function (url) { return MATCH_URL.test(url); }; UstreamVideo.loopOnEnded = false; exports['default'] = (0, _singlePlayer2['default'])(UstreamVideo);