UNPKG

stream-chat-react-native-core

Version:

The official React Native and Expo components for Stream Chat, a service for building chat applications

417 lines 13.2 kB
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.AudioPlayer = void 0; var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _streamChat = require("stream-chat"); var _native = require("../native"); var DEFAULT_PLAYBACK_RATES = [1.0, 1.5, 2.0]; var DEFAULT_PLAYER_SETTINGS = { pitchCorrectionQuality: 'high', progressUpdateIntervalMillis: 100, shouldCorrectPitch: true }; var INITIAL_STATE = { currentPlaybackRate: 1.0, duration: 0, isPlaying: false, playbackRates: DEFAULT_PLAYBACK_RATES, position: 0, progress: 0 }; class AudioPlayer { playerRef = null; initRequestId = 0; _pool = null; constructor(options) { var _options$previewVoice, _options$playbackRate; this.isExpoCLI = _native.NativeHandlers.SDK === 'stream-chat-expo'; this._id = options.id; this.type = options.type; this.onError = options.onError; this.previewVoiceRecording = (_options$previewVoice = options.previewVoiceRecording) != null ? _options$previewVoice : false; var playbackRates = (_options$playbackRate = options.playbackRates) != null ? _options$playbackRate : DEFAULT_PLAYBACK_RATES; this.state = new _streamChat.StateStore({ ...INITIAL_STATE, currentPlaybackRate: playbackRates[0], duration: options.duration * 1000, playbackRates }); this.initPlayer({ uri: options.uri }); } setOnError(onError) { this.onError = onError; } getError(error) { if (error instanceof Error) return error; if (typeof error === 'string') return new Error(error); if (error && typeof error === 'object' && 'message' in error) { var message = error.message; if (typeof message === 'string') return new Error(message); } return undefined; } notifyError(errCode, error) { var _this$onError; if (errCode === 'seek-not-supported') { var now = Date.now(); if (typeof this.lastSeekNotSupportedNotificationAt === 'number' && now - this.lastSeekNotSupportedNotificationAt < 1000) { return; } this.lastSeekNotSupportedNotificationAt = now; } (_this$onError = this.onError) == null || _this$onError.call(this, { errCode, error: this.getError(error) }); } handleMaybePromiseError(result, errCode, onError) { if (!result || typeof result.catch !== 'function') return; result.catch(error => { onError == null || onError(); this.notifyError(errCode, error); }); } initPlayer = (() => { var _this = this; return function () { var _ref = (0, _asyncToGenerator2.default)(function* ({ uri, playerRef }) { var _NativeHandlers$Sound; var requestId = ++_this.initRequestId; if (playerRef) { if (requestId === _this.initRequestId) { _this.playerRef = playerRef; } return; } if (_this.previewVoiceRecording) { var _NativeHandlers$Audio; if ((_NativeHandlers$Audio = _native.NativeHandlers.Audio) != null && _NativeHandlers$Audio.startPlayer) { try { var _NativeHandlers$Audio2; yield _native.NativeHandlers.Audio.startPlayer(uri, {}, _this.onVoiceRecordingPreviewPlaybackStatusUpdate); if ((_NativeHandlers$Audio2 = _native.NativeHandlers.Audio) != null && _NativeHandlers$Audio2.pausePlayer) { yield _native.NativeHandlers.Audio.pausePlayer(); } } catch (error) { _this.notifyError('failed-to-start', error); } } return; } if (!uri || !((_NativeHandlers$Sound = _native.NativeHandlers.Sound) != null && _NativeHandlers$Sound.initializeSound)) { return; } var player; try { player = yield _native.NativeHandlers.Sound.initializeSound({ uri }, DEFAULT_PLAYER_SETTINGS, _this.onPlaybackStatusUpdate); } catch (error) { _this.notifyError('failed-to-start', error); return; } if (!player) { _this.notifyError('not-playable'); return; } if (requestId !== _this.initRequestId) { var _player, _player2; if ((_player = player) != null && _player.stopAsync) { player.stopAsync(); } if ((_player2 = player) != null && _player2.unloadAsync) { player.unloadAsync(); } return; } _this.playerRef = player; }); return function (_x) { return _ref.apply(this, arguments); }; }(); })(); onVoiceRecordingPreviewPlaybackStatusUpdate = (() => { var _this2 = this; return function () { var _ref2 = (0, _asyncToGenerator2.default)(function* (playbackStatus) { var currentProgress = playbackStatus.currentPosition / playbackStatus.duration; if (currentProgress === 1) { yield _this2.stop(); } else { _this2.progress = currentProgress; } }); return function (_x2) { return _ref2.apply(this, arguments); }; }(); })(); onPlaybackStatusUpdate = (() => { var _this3 = this; return function () { var _ref3 = (0, _asyncToGenerator2.default)(function* (playbackStatus) { if (!playbackStatus.isLoaded) { if (playbackStatus.error) { if (_this3.lastPlaybackStatusError !== playbackStatus.error) { _this3.lastPlaybackStatusError = playbackStatus.error; _this3.notifyError('not-playable', playbackStatus.error); } } } else { var durationMillis = playbackStatus.durationMillis, positionMillis = playbackStatus.positionMillis; if (_this3.type !== 'voiceRecording') { _this3.duration = durationMillis; } if (playbackStatus.isPlaying) { var duration = _this3.type === 'voiceRecording' ? _this3.duration : durationMillis; if (positionMillis <= duration) { _this3.position = positionMillis; } } if (playbackStatus.didJustFinish && !playbackStatus.isLooping) { yield _this3.stop(); } } }); return function (_x3) { return _ref3.apply(this, arguments); }; }(); })(); get isPlaying() { return this.state.getLatestValue().isPlaying; } get duration() { return this.state.getLatestValue().duration; } get position() { return this.state.getLatestValue().position; } get progress() { return this.state.getLatestValue().progress; } get playbackRates() { return this.state.getLatestValue().playbackRates; } get currentPlaybackRate() { return this.state.getLatestValue().currentPlaybackRate; } get id() { return this._id; } set pool(pool) { this._pool = pool; } set duration(duration) { this.state.partialNext({ duration }); } set position(position) { this.state.partialNext({ position, progress: position / this.duration }); } set progress(progress) { this.state.partialNext({ position: progress * this.duration, progress }); } set isPlaying(isPlaying) { this.state.partialNext({ isPlaying }); } changePlaybackRate() { var _this4 = this; return (0, _asyncToGenerator2.default)(function* () { var _this4$playerRef; var currentPlaybackRateIndex = _this4.state.getLatestValue().playbackRates.indexOf(_this4.currentPlaybackRate); if (currentPlaybackRateIndex === -1) { currentPlaybackRateIndex = 0; } var nextPlayBackIndex = currentPlaybackRateIndex === _this4.playbackRates.length - 1 ? 0 : currentPlaybackRateIndex + 1; var nextPlaybackRate = _this4.playbackRates[nextPlayBackIndex]; _this4.state.partialNext({ currentPlaybackRate: nextPlaybackRate }); if (!_this4.playerRef) { return; } if ((_this4$playerRef = _this4.playerRef) != null && _this4$playerRef.setRateAsync) { yield _this4.playerRef.setRateAsync(nextPlaybackRate, true, 'high'); } })(); } play() { if (this.isPlaying) { return; } if (this._pool) { this._pool.requestPlay(this.id); } if (this.previewVoiceRecording) { var _NativeHandlers$Audio3; if ((_NativeHandlers$Audio3 = _native.NativeHandlers.Audio) != null && _NativeHandlers$Audio3.resumePlayer) { _native.NativeHandlers.Audio.resumePlayer().catch(error => { this.isPlaying = false; this.notifyError('failed-to-start', error); }); } this.state.partialNext({ isPlaying: true }); return; } if (!this.playerRef) { this.notifyError('not-playable'); return; } if (this.isExpoCLI) { var _this$playerRef; if ((_this$playerRef = this.playerRef) != null && _this$playerRef.playAsync) { this.handleMaybePromiseError(this.playerRef.playAsync(), 'failed-to-start', () => { this.isPlaying = false; }); } } else { var _this$playerRef2; if ((_this$playerRef2 = this.playerRef) != null && _this$playerRef2.resume) { try { this.playerRef.resume(); } catch (error) { this.notifyError('failed-to-start', error); return; } } } this.state.partialNext({ isPlaying: true }); } pause() { if (!this.isPlaying) { return; } if (this.previewVoiceRecording) { var _NativeHandlers$Audio4; if ((_NativeHandlers$Audio4 = _native.NativeHandlers.Audio) != null && _NativeHandlers$Audio4.pausePlayer) { _native.NativeHandlers.Audio.pausePlayer(); } this.state.partialNext({ isPlaying: false }); return; } if (!this.playerRef) { return; } if (this.isExpoCLI) { var _this$playerRef3; if ((_this$playerRef3 = this.playerRef) != null && _this$playerRef3.pauseAsync) { this.playerRef.pauseAsync(); } } else { var _this$playerRef4; if ((_this$playerRef4 = this.playerRef) != null && _this$playerRef4.pause) { this.playerRef.pause(); } } this.state.partialNext({ isPlaying: false }); if (this._pool) { this._pool.notifyPaused(); } } toggle() { if (this.isPlaying) { this.pause(); } else { this.play(); } } seek(positionInSeconds) { var _this5 = this; return (0, _asyncToGenerator2.default)(function* () { var positionInMillis = positionInSeconds * 1000; if (_this5.previewVoiceRecording) { var _NativeHandlers$Audio5; _this5.position = positionInMillis; if ((_NativeHandlers$Audio5 = _native.NativeHandlers.Audio) != null && _NativeHandlers$Audio5.seekToPlayer) { yield _native.NativeHandlers.Audio.seekToPlayer(positionInMillis); } else if (positionInMillis > 0) { _this5.notifyError('seek-not-supported'); } return; } if (!_this5.playerRef) { return; } _this5.position = positionInMillis; if (_this5.isExpoCLI) { var _this5$playerRef; if ((_this5$playerRef = _this5.playerRef) != null && _this5$playerRef.setPositionAsync) { yield _this5.playerRef.setPositionAsync(positionInMillis); } else { _this5.notifyError('seek-not-supported'); } } else { var _this5$playerRef2; if ((_this5$playerRef2 = _this5.playerRef) != null && _this5$playerRef2.seek) { _this5.playerRef.seek(positionInSeconds); } else if (positionInMillis > 0) { _this5.notifyError('seek-not-supported'); } } })(); } stop() { var _this6 = this; return (0, _asyncToGenerator2.default)(function* () { yield _this6.seek(0); _this6.pause(); })(); } onRemove() { var _this$playerRef5, _this$playerRef6; this.initRequestId += 1; if (this.previewVoiceRecording) { var _NativeHandlers$Audio6; if ((_NativeHandlers$Audio6 = _native.NativeHandlers.Audio) != null && _NativeHandlers$Audio6.stopPlayer) { _native.NativeHandlers.Audio.stopPlayer(); } this.state.partialNext({ ...INITIAL_STATE, currentPlaybackRate: this.playbackRates[0], playbackRates: DEFAULT_PLAYBACK_RATES }); return; } if ((_this$playerRef5 = this.playerRef) != null && _this$playerRef5.stopAsync) { this.playerRef.stopAsync(); } if ((_this$playerRef6 = this.playerRef) != null && _this$playerRef6.unloadAsync) { this.playerRef.unloadAsync(); } this.playerRef = null; this.state.partialNext({ ...INITIAL_STATE, currentPlaybackRate: this.playbackRates[0], playbackRates: DEFAULT_PLAYBACK_RATES }); } } exports.AudioPlayer = AudioPlayer; //# sourceMappingURL=audio-player.js.map