react-player-custom
Version:
A React component for playing a variety of URLs, including file paths, YouTube, Facebook, Twitch, SoundCloud, Streamable, Vimeo, Wistia and DailyMotion
165 lines (139 loc) • 6.22 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SoundCloud = undefined;
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 _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://w.soundcloud.com/player/api.js';
var SDK_GLOBAL = 'SC';
var MATCH_URL = /(soundcloud\.com|snd\.sc)\/.+$/;
var SoundCloud = exports.SoundCloud = function (_Component) {
_inherits(SoundCloud, _Component);
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.callPlayer = _utils.callPlayer, _this.duration = null, _this.currentTime = null, _this.fractionLoaded = null, _this.mute = function () {
_this.setVolume(0);
}, _this.unmute = function () {
if (_this.props.volume !== null) {
_this.setVolume(_this.props.volume);
}
}, _this.ref = function (iframe) {
_this.iframe = iframe;
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(SoundCloud, [{
key: 'load',
value: function load(url, isReady) {
var _this2 = this;
(0, _utils.getSDK)(SDK_URL, SDK_GLOBAL).then(function (SC) {
if (!_this2.iframe) return;
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 (!isReady) {
_this2.player = SC.Widget(_this2.iframe);
_this2.player.bind(PLAY, _this2.props.onPlay);
_this2.player.bind(PAUSE, _this2.props.onPause);
_this2.player.bind(PLAY_PROGRESS, function (e) {
_this2.currentTime = e.currentPosition / 1000;
_this2.fractionLoaded = e.loadedProgress;
});
_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({}, _this2.props.config.soundcloud.options, {
callback: function callback() {
_this2.player.getDuration(function (duration) {
_this2.duration = duration / 1000;
_this2.props.onReady();
});
}
}));
});
}
}, {
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('seekTo', seconds * 1000);
}
}, {
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.fractionLoaded * this.duration;
}
}, {
key: 'render',
value: function render() {
var style = _extends({
width: '100%',
height: '100%'
}, this.props.style);
return _react2['default'].createElement('iframe', {
ref: this.ref,
src: 'https://w.soundcloud.com/player/?url=' + encodeURIComponent(this.props.url),
style: style,
frameBorder: 0,
allow: 'autoplay'
});
}
}]);
return SoundCloud;
}(_react.Component);
SoundCloud.displayName = 'SoundCloud';
SoundCloud.canPlay = function (url) {
return MATCH_URL.test(url);
};
SoundCloud.loopOnEnded = true;
exports['default'] = (0, _singlePlayer2['default'])(SoundCloud);
;