react-player
Version:
A react component for playing a variety of URLs, including file paths, YouTube, Facebook, SoundCloud, Streamable, Vidme, Vimeo and Wistia
186 lines (158 loc) • 7.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
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 _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _Base2 = require('./Base');
var _Base3 = _interopRequireDefault(_Base2);
var _utils = require('../utils');
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://w.soundcloud.com/player/api.js';
var SDK_GLOBAL = 'SC';
var MATCH_URL = /^https?:\/\/(soundcloud.com|snd.sc)\/([a-z0-9-_]+\/[a-z0-9-_]+)$/;
var DEFAULT_OPTIONS = {
visual: true, // Undocumented, but makes player fill container and look better
buying: false,
liking: false,
download: false,
sharing: false,
show_comments: false,
show_playcount: false
};
var SoundCloud = function (_Base) {
_inherits(SoundCloud, _Base);
function SoundCloud() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, SoundCloud);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = SoundCloud.__proto__ || Object.getPrototypeOf(SoundCloud)).call.apply(_ref, [this].concat(args))), _this), _this.player = null, _this.duration = null, _this.fractionPlayed = null, _this.fractionLoaded = null, _this.ref = function (iframe) {
_this.iframe = iframe;
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(SoundCloud, [{
key: 'load',
value: function load(url) {
var _this2 = this;
(0, _utils.getSDK)(SDK_URL, SDK_GLOBAL).then(function (SC) {
var _SC$Widget$Events = SC.Widget.Events,
PLAY = _SC$Widget$Events.PLAY,
PLAY_PROGRESS = _SC$Widget$Events.PLAY_PROGRESS,
PAUSE = _SC$Widget$Events.PAUSE,
FINISH = _SC$Widget$Events.FINISH,
ERROR = _SC$Widget$Events.ERROR;
if (!_this2.isReady) {
_this2.player = SC.Widget(_this2.iframe);
_this2.player.bind(PLAY, _this2.onPlay);
_this2.player.bind(PLAY_PROGRESS, function (e) {
_this2.fractionPlayed = e.relativePosition;
_this2.fractionLoaded = e.loadedProgress;
});
_this2.player.bind(PAUSE, function () {
return _this2.props.onPause();
});
_this2.player.bind(FINISH, function () {
return _this2.props.onEnded();
});
_this2.player.bind(ERROR, function (e) {
return _this2.props.onError(e);
});
}
_this2.player.load(url, _extends({}, DEFAULT_OPTIONS, _this2.props.config.soundcloud.options, {
callback: function callback() {
_this2.player.getDuration(function (duration) {
_this2.duration = duration / 1000;
_this2.onReady();
});
}
}));
});
}
}, {
key: 'call',
value: function call(method) {
var _player;
if (!this.isReady || !this.player || !this.player[method]) return;
for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
args[_key2 - 1] = arguments[_key2];
}
return (_player = this.player)[method].apply(_player, args);
}
}, {
key: 'play',
value: function play() {
this.call('play');
}
}, {
key: 'pause',
value: function pause() {
this.call('pause');
}
}, {
key: 'stop',
value: function stop() {
this.pause();
this.call('seekTo', 0);
}
}, {
key: 'seekTo',
value: function seekTo(amount) {
var seconds = _get(SoundCloud.prototype.__proto__ || Object.getPrototypeOf(SoundCloud.prototype), 'seekTo', this).call(this, amount);
this.call('seekTo', seconds * 1000);
}
}, {
key: 'setVolume',
value: function setVolume(fraction) {
this.call('setVolume', fraction * 100);
}
}, {
key: 'setPlaybackRate',
value: function setPlaybackRate() {}
}, {
key: 'getDuration',
value: function getDuration() {
return this.duration;
}
}, {
key: 'getFractionLoaded',
value: function getFractionLoaded() {
return this.fractionLoaded;
}
}, {
key: 'getFractionPlayed',
value: function getFractionPlayed() {
return this.fractionPlayed;
}
}, {
key: 'render',
value: function render() {
var style = {
width: '100%',
height: '100%'
};
return _react2['default'].createElement('iframe', {
ref: this.ref,
src: 'https://w.soundcloud.com/player/?url=' + encodeURIComponent(this.props.url),
style: style,
frameBorder: 0
});
}
}], [{
key: 'canPlay',
value: function canPlay(url) {
return MATCH_URL.test(url);
}
}]);
return SoundCloud;
}(_Base3['default']);
SoundCloud.displayName = 'SoundCloud';
exports['default'] = SoundCloud;