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
150 lines (124 loc) • 5.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Streamable = 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 = '//cdn.embed.ly/player-0.1.0.min.js';
var SDK_GLOBAL = 'playerjs';
var MATCH_URL = /streamable\.com\/([a-z0-9]+)$/;
var Streamable = exports.Streamable = function (_Component) {
_inherits(Streamable, _Component);
function Streamable() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, Streamable);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Streamable.__proto__ || Object.getPrototypeOf(Streamable)).call.apply(_ref, [this].concat(args))), _this), _this.callPlayer = _utils.callPlayer, _this.duration = null, _this.currentTime = null, _this.secondsLoaded = null, _this.ref = function (iframe) {
_this.iframe = iframe;
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(Streamable, [{
key: 'load',
value: function load(url) {
var _this2 = this;
(0, _utils.getSDK)(SDK_URL, SDK_GLOBAL).then(function (playerjs) {
if (!_this2.iframe) return;
_this2.player = new playerjs.Player(_this2.iframe);
_this2.player.setLoop(_this2.props.loop);
_this2.player.on('ready', _this2.props.onReady);
_this2.player.on('play', _this2.props.onPlay);
_this2.player.on('pause', _this2.props.onPause);
_this2.player.on('seeked', _this2.props.onSeek);
_this2.player.on('ended', _this2.props.onEnded);
_this2.player.on('error', _this2.props.onError);
_this2.player.on('timeupdate', function (_ref2) {
var duration = _ref2.duration,
seconds = _ref2.seconds;
_this2.duration = duration;
_this2.currentTime = seconds;
});
_this2.player.on('buffered', function (_ref3) {
var percent = _ref3.percent;
if (_this2.duration) {
_this2.secondsLoaded = _this2.duration * percent;
}
});
}, this.props.onError);
}
}, {
key: 'play',
value: function play() {
this.callPlayer('play');
}
}, {
key: 'pause',
value: function pause() {
this.callPlayer('pause');
}
}, {
key: 'stop',
value: function stop() {
// Nothing to do
}
}, {
key: 'seekTo',
value: function seekTo(seconds) {
this.callPlayer('setCurrentTime', seconds);
}
}, {
key: 'setVolume',
value: function setVolume(fraction) {
this.callPlayer('setVolume', fraction * 100);
}
}, {
key: 'getDuration',
value: function getDuration() {
return this.duration;
}
}, {
key: 'getCurrentTime',
value: function getCurrentTime() {
return this.currentTime;
}
}, {
key: 'getSecondsLoaded',
value: function getSecondsLoaded() {
return this.secondsLoaded;
}
}, {
key: 'render',
value: function render() {
var id = this.props.url.match(MATCH_URL)[1];
var style = {
width: '100%',
height: '100%'
};
return _react2['default'].createElement('iframe', {
ref: this.ref,
src: 'https://streamable.com/o/' + id,
frameBorder: '0',
scrolling: 'no',
style: style,
allowFullScreen: true
});
}
}]);
return Streamable;
}(_react.Component);
Streamable.displayName = 'Streamable';
Streamable.canPlay = function (url) {
return MATCH_URL.test(url);
};
exports['default'] = (0, _singlePlayer2['default'])(Streamable);