react-player
Version:
A react component for playing a variety of URLs, including file paths, YouTube, Facebook, SoundCloud, Streamable, Vidme, Vimeo and Wistia
77 lines (58 loc) • 3.25 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 _FilePlayer2 = require('./FilePlayer');
var _FilePlayer3 = _interopRequireDefault(_FilePlayer2);
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 RESOLVE_URL = 'https://api.streamable.com/videos/';
var MATCH_URL = /^https?:\/\/streamable.com\/([a-z0-9]+)$/;
var cache = {}; // Cache song data requests
var Streamable = function (_FilePlayer) {
_inherits(Streamable, _FilePlayer);
function Streamable() {
_classCallCheck(this, Streamable);
return _possibleConstructorReturn(this, (Streamable.__proto__ || Object.getPrototypeOf(Streamable)).apply(this, arguments));
}
_createClass(Streamable, [{
key: 'getData',
value: function getData(url) {
var onError = this.props.onError;
var id = url.match(MATCH_URL)[1];
if (cache[id]) {
return Promise.resolve(cache[id]);
}
return window.fetch(RESOLVE_URL + id).then(function (response) {
if (response.status === 200) {
cache[id] = response.json();
return cache[id];
} else {
onError(new Error('Streamable track could not be resolved'));
}
});
}
}, {
key: 'load',
value: function load(url) {
var _this2 = this;
var onError = this.props.onError;
this.stop();
this.getData(url).then(function (data) {
if (!_this2.mounted) return;
_this2.player.src = data.files.mp4.url;
}, onError);
}
}], [{
key: 'canPlay',
value: function canPlay(url) {
return MATCH_URL.test(url);
}
}]);
return Streamable;
}(_FilePlayer3['default']);
Streamable.displayName = 'Streamable';
exports['default'] = Streamable;