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
172 lines (146 loc) • 5.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Iframe = 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 MATCH_URL = /^(https?:\/\/)?([\da-z\\.-]+)\.([a-z\\.]{2,6})([\\/\w \\.-]*)*\/?$/;
var PLAYER_ID_PREFIX = 'Iframe-player-';
var Iframe = exports.Iframe = function (_Component) {
_inherits(Iframe, _Component);
function Iframe() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, Iframe);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Iframe.__proto__ || Object.getPrototypeOf(Iframe)).call.apply(_ref, [this].concat(args))), _this), _this.playerID = PLAYER_ID_PREFIX + (0, _utils.randomString)(), _this.ref = function (container) {
_this.container = container;
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(Iframe, [{
key: 'load',
value: function load(url) {
this.player = {
currentTime: 0
};
this.props.onReady();
}
}, {
key: 'play',
value: function play() {
var _this2 = this;
this.playTime = Date.now();
setTimeout(function () {
return _this2.props.onPlay();
}, 1);
}
}, {
key: 'pause',
value: function pause() {
var _this3 = this;
this.player.currentTime = this.getCurrentTime();
this.playTime = null;
setTimeout(function () {
return _this3.props.onPause();
}, 1);
}
}, {
key: 'stop',
value: function stop() {
this.player.currentTime = this.getCurrentTime();
this.playTime = null;
this.props.onPause();
}
}, {
key: 'seekTo',
value: function seekTo(seconds) {
// no support
}
}, {
key: 'setVolume',
value: function setVolume(fraction) {
// no support
}
}, {
key: 'getDuration',
value: function getDuration() {
return Infinity;
}
}, {
key: 'getCurrentTime',
value: function getCurrentTime() {
var playing = 0;
if (this.playTime) {
playing = (Date.now() - this.playTime) / 1000;
}
return this.player.currentTime + playing;
}
}, {
key: 'getSecondsLoaded',
value: function getSecondsLoaded() {
return null;
}
}, {
key: 'render',
value: function render() {
var style = {
width: '100%',
height: '100%'
};
var _props = this.props,
url = _props.url,
playing = _props.playing;
if (playing) {
return _react2['default'].createElement('iframe', {
id: this.playerID,
ref: this.ref,
src: playing && url,
frameBorder: '0',
scrolling: 'no',
style: style,
allowFullScreen: true
});
} else {
// pause flow for iframe
return _react2['default'].createElement(
'div',
{ style: style },
_react2['default'].createElement(
'div',
{ style: {
alignItems: 'center',
background: 'rgba(255,255,255,0.3)',
display: 'flex',
height: '100%',
justifyContent: 'center',
width: '100%'
} },
_react2['default'].createElement('div', { className: 'pause', style: {
borderStyle: 'double',
borderWidth: '0px 0px 0px 50px',
color: 'gray',
height: '60px'
} })
)
);
}
}
}]);
return Iframe;
}(_react.Component);
Iframe.displayName = 'Iframe';
Iframe.canPlay = function (url) {
return MATCH_URL.test(url);
};
exports['default'] = (0, _singlePlayer2['default'])(Iframe);