UNPKG

@teyuto/react-native-player

Version:
259 lines (197 loc) 7.58 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var React = require('react'); var React__default = _interopDefault(React); var reactNativeWebview = require('react-native-webview'); function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } var PLAYER_HOST = 'https://teyuto.tv/video/player?w='; var DEFAULT_STYLE = { width: '100%', height: '100%' }; var TeyutoPlayerSdk = /*#__PURE__*/function (_Component) { _inheritsLoose(TeyutoPlayerSdk, _Component); function TeyutoPlayerSdk(props) { var _this; _this = _Component.call(this, props) || this; _this.playerUrl = _this.buildEmbedUrl(props); _this.token = _this.buildToken(props); return _this; } var _proto = TeyutoPlayerSdk.prototype; _proto.play = function play() { var _this$webref; (_this$webref = this.webref) == null ? void 0 : _this$webref.injectJavaScript("player.play(); true;"); }; _proto.pause = function pause() { var _this$webref2; (_this$webref2 = this.webref) == null ? void 0 : _this$webref2.injectJavaScript("player.pause(); true;"); }; _proto.requestFullscreen = function requestFullscreen() { var _this$webref3; (_this$webref3 = this.webref) == null ? void 0 : _this$webref3.injectJavaScript("player.requestFullscreen(); true;"); }; _proto.mute = function mute() { var _this$webref4; (_this$webref4 = this.webref) == null ? void 0 : _this$webref4.injectJavaScript("player.muted(true); true;"); }; _proto.unmute = function unmute() { var _this$webref5; (_this$webref5 = this.webref) == null ? void 0 : _this$webref5.injectJavaScript("player.muted(false); true;"); }; _proto.seek = function seek(time) { var _this$webref6; if (isNaN(time)) throw new Error('Invalid time'); (_this$webref6 = this.webref) == null ? void 0 : _this$webref6.injectJavaScript("player.currentTime(player.currentTime() + " + time + "); true;"); }; _proto.setCurrentTime = function setCurrentTime(time) { var _this$webref7; if (isNaN(time)) throw new Error('Invalid time'); (_this$webref7 = this.webref) == null ? void 0 : _this$webref7.injectJavaScript("player.currentTime(" + time + "); true;"); }; _proto.setLoop = function setLoop(loop) { var _this$webref8; (_this$webref8 = this.webref) == null ? void 0 : _this$webref8.injectJavaScript("player.loop(" + !!loop + "); true;"); }; _proto.setPlaybackRate = function setPlaybackRate(rate) { var _this$webref9; if (isNaN(rate)) throw new Error('Invalid rate'); (_this$webref9 = this.webref) == null ? void 0 : _this$webref9.injectJavaScript("player.playbackRate(" + rate + "); true;"); }; _proto.setVolume = function setVolume(volume) { var _this$webref10; if (isNaN(volume)) throw new Error('Invalid volume'); (_this$webref10 = this.webref) == null ? void 0 : _this$webref10.injectJavaScript("player.volume(" + volume + "); true;"); }; _proto.onMessage = function onMessage(message) { console.log(message); if (!message['type']) { return; } switch (message['type']) { case 'controlsdisabled': if (this.props.onControlsDisabled) this.props.onControlsDisabled(); break; case 'controlsenabled': if (this.props.onControlsEnabled) this.props.onControlsEnabled(); break; case 'ended': if (this.props.onEnded) this.props.onEnded(); break; case 'error': if (this.props.onError) this.props.onError(); break; case 'firstplay': if (this.props.onFirstPlay) this.props.onFirstPlay(); break; case 'fullscreenchange': if (this.props.onFullScreenChange) this.props.onFullScreenChange(); break; case 'pause': if (this.props.onPause) this.props.onPause(); break; case 'play': if (this.props.onPlay) this.props.onPlay(); break; case 'playerresize': if (this.props.onPlayerResize) this.props.onPlayerResize(); break; case 'qualitychange': if (this.props.onQualityChange) this.props.onQualityChange(message.resolution); break; case 'ratechange': if (this.props.onRateChange) this.props.onRateChange(); break; case 'ready': if (this.props.onReady) this.props.onReady(); break; case 'resize': if (this.props.onResize) this.props.onResize(); break; case 'seeking': if (this.props.onSeeking) this.props.onSeeking(); break; case 'timeupdate': if (this.props.onTimeUpdate) this.props.onTimeUpdate(message.data.currentTime); break; case 'useractive': if (this.props.onUserActive) this.props.onUserActive(); break; case 'userinactive': if (this.props.onUserInactive) this.props.onUserInactive(); break; case 'volumechange': if (this.props.onVolumeChange) this.props.onVolumeChange(message.data.volume); break; } }; _proto.buildQueryParameters = function buildQueryParameters(originalProps) { var query = '&pip=off'; var keyToQueryParameMap = { autoplay: 'auto', muted: 'muted', controls: 'controls', playbackRates: 'playbackRates', qualitySelector: 'qualitySelector', seekButtons: 'seekButtons', playerColor: 'playerColor', loop: 'loop', captions: 'captions', lowLatency: 'lowLatency' }; for (var key in originalProps) { if (key === 'autoplay' || key === 'muted' || key === 'controls' || key === 'playbackRates' || key === 'qualitySelector' || key === 'seekButtons' || key === 'playerColor' || key === 'loop' || key === 'captions' || key === 'lowLatency') { query += "&" + keyToQueryParameMap[key] + "=" + originalProps[key]; } } return query; }; _proto.buildEmbedUrl = function buildEmbedUrl(props) { var url = "" + PLAYER_HOST + props.id; url += this.buildQueryParameters(props); return url; }; _proto.buildToken = function buildToken(props) { var token = ''; token += props.token; return token; }; _proto.render = function render() { var _this2 = this; return React__default.createElement(reactNativeWebview.WebView, { ref: function ref(r) { return _this2.webref = r; }, source: { uri: this.playerUrl, headers: { Authorization: 'Bearer ' + this.token } }, style: this.props.style || DEFAULT_STYLE, scrollEnabled: false, onMessage: function onMessage(msg) { return _this2.onMessage(JSON.parse(msg.nativeEvent.data)); }, allowsInlineMediaPlayback: true, allowsFullscreenVideo: true, mediaPlaybackRequiresUserAction: false, injectedJavaScriptBeforeContentLoaded: "window.addEventListener('message', (m) => window.ReactNativeWebView.postMessage(JSON.stringify(m.data)))" }); }; return TeyutoPlayerSdk; }(React.Component); exports.default = TeyutoPlayerSdk; //# sourceMappingURL=react-native-player.cjs.development.js.map