UNPKG

stream-chat-react-native-core

Version:

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

441 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 _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); 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 }; var AudioPlayer = exports.AudioPlayer = function () { function AudioPlayer(options) { var _this = this; (0, _classCallCheck2.default)(this, AudioPlayer); this.playerRef = null; this.initRequestId = 0; this._pool = null; this.initPlayer = function () { var _ref = (0, _asyncToGenerator2.default)(function* ({ uri, playerRef }) { var requestId = ++_this.initRequestId; if (playerRef) { if (requestId === _this.initRequestId) { _this.playerRef = playerRef; } return; } if (_this.previewVoiceRecording) { if (_native.NativeHandlers.Audio?.startPlayer) { try { yield _native.NativeHandlers.Audio.startPlayer(uri, {}, _this.onVoiceRecordingPreviewPlaybackStatusUpdate); if (_native.NativeHandlers.Audio?.pausePlayer) { yield _native.NativeHandlers.Audio.pausePlayer(); } } catch (error) { _this.notifyError('failed-to-start', error); } } return; } if (!uri || !_native.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) { if (player?.stopAsync) { player.stopAsync(); } if (player?.unloadAsync) { player.unloadAsync(); } return; } _this.playerRef = player; }); return function (_x) { return _ref.apply(this, arguments); }; }(); this.onVoiceRecordingPreviewPlaybackStatusUpdate = function () { var _ref2 = (0, _asyncToGenerator2.default)(function* (playbackStatus) { var currentProgress = playbackStatus.currentPosition / playbackStatus.duration; if (currentProgress === 1) { yield _this.stop(); } else { _this.progress = currentProgress; } }); return function (_x2) { return _ref2.apply(this, arguments); }; }(); this.onPlaybackStatusUpdate = function () { var _ref3 = (0, _asyncToGenerator2.default)(function* (playbackStatus) { if (!playbackStatus.isLoaded) { if (playbackStatus.error) { if (_this.lastPlaybackStatusError !== playbackStatus.error) { _this.lastPlaybackStatusError = playbackStatus.error; _this.notifyError('not-playable', playbackStatus.error); } } } else { var durationMillis = playbackStatus.durationMillis, positionMillis = playbackStatus.positionMillis; if (_this.type !== 'voiceRecording') { _this.duration = durationMillis; } if (playbackStatus.isPlaying) { var duration = _this.type === 'voiceRecording' ? _this.duration : durationMillis; if (positionMillis <= duration) { _this.position = positionMillis; } } if (playbackStatus.didJustFinish && !playbackStatus.isLooping) { yield _this.stop(); } } }); return function (_x3) { return _ref3.apply(this, arguments); }; }(); this.isExpoCLI = _native.NativeHandlers.SDK === 'stream-chat-expo'; this._id = options.id; this.type = options.type; this.onError = options.onError; this.previewVoiceRecording = options.previewVoiceRecording ?? false; var playbackRates = options.playbackRates ?? DEFAULT_PLAYBACK_RATES; this.state = new _streamChat.StateStore({ ...INITIAL_STATE, currentPlaybackRate: playbackRates[0], duration: options.duration * 1000, playbackRates }); this.initPlayer({ uri: options.uri }); } return (0, _createClass2.default)(AudioPlayer, [{ key: "setOnError", value: function setOnError(onError) { this.onError = onError; } }, { key: "getError", value: function 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; } }, { key: "notifyError", value: function notifyError(errCode, error) { if (errCode === 'seek-not-supported') { var now = Date.now(); if (typeof this.lastSeekNotSupportedNotificationAt === 'number' && now - this.lastSeekNotSupportedNotificationAt < 1000) { return; } this.lastSeekNotSupportedNotificationAt = now; } this.onError?.({ errCode, error: this.getError(error) }); } }, { key: "handleMaybePromiseError", value: function handleMaybePromiseError(result, errCode, onError) { if (!result || typeof result.catch !== 'function') return; result.catch(error => { onError?.(); this.notifyError(errCode, error); }); } }, { key: "isPlaying", get: function () { return this.state.getLatestValue().isPlaying; }, set: function (isPlaying) { this.state.partialNext({ isPlaying }); } }, { key: "duration", get: function () { return this.state.getLatestValue().duration; }, set: function (duration) { this.state.partialNext({ duration }); } }, { key: "position", get: function () { return this.state.getLatestValue().position; }, set: function (position) { this.state.partialNext({ position, progress: position / this.duration }); } }, { key: "progress", get: function () { return this.state.getLatestValue().progress; }, set: function (progress) { this.state.partialNext({ position: progress * this.duration, progress }); } }, { key: "playbackRates", get: function () { return this.state.getLatestValue().playbackRates; } }, { key: "currentPlaybackRate", get: function () { return this.state.getLatestValue().currentPlaybackRate; } }, { key: "id", get: function () { return this._id; } }, { key: "pool", set: function (pool) { this._pool = pool; } }, { key: "changePlaybackRate", value: function () { var _changePlaybackRate = (0, _asyncToGenerator2.default)(function* () { var currentPlaybackRateIndex = this.state.getLatestValue().playbackRates.indexOf(this.currentPlaybackRate); if (currentPlaybackRateIndex === -1) { currentPlaybackRateIndex = 0; } var nextPlayBackIndex = currentPlaybackRateIndex === this.playbackRates.length - 1 ? 0 : currentPlaybackRateIndex + 1; var nextPlaybackRate = this.playbackRates[nextPlayBackIndex]; this.state.partialNext({ currentPlaybackRate: nextPlaybackRate }); if (!this.playerRef) { return; } if (this.playerRef?.setRateAsync) { yield this.playerRef.setRateAsync(nextPlaybackRate, true, 'high'); } }); function changePlaybackRate() { return _changePlaybackRate.apply(this, arguments); } return changePlaybackRate; }() }, { key: "play", value: function play() { if (this.isPlaying) { return; } if (this._pool) { this._pool.requestPlay(this.id); } if (this.previewVoiceRecording) { if (_native.NativeHandlers.Audio?.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) { if (this.playerRef?.playAsync) { this.handleMaybePromiseError(this.playerRef.playAsync(), 'failed-to-start', () => { this.isPlaying = false; }); } } else { if (this.playerRef?.resume) { try { this.playerRef.resume(); } catch (error) { this.notifyError('failed-to-start', error); return; } } } this.state.partialNext({ isPlaying: true }); } }, { key: "pause", value: function pause() { if (!this.isPlaying) { return; } if (this.previewVoiceRecording) { if (_native.NativeHandlers.Audio?.pausePlayer) { _native.NativeHandlers.Audio.pausePlayer(); } this.state.partialNext({ isPlaying: false }); return; } if (!this.playerRef) { return; } if (this.isExpoCLI) { if (this.playerRef?.pauseAsync) { this.playerRef.pauseAsync(); } } else { if (this.playerRef?.pause) { this.playerRef.pause(); } } this.state.partialNext({ isPlaying: false }); if (this._pool) { this._pool.notifyPaused(); } } }, { key: "toggle", value: function toggle() { if (this.isPlaying) { this.pause(); } else { this.play(); } } }, { key: "seek", value: function () { var _seek = (0, _asyncToGenerator2.default)(function* (positionInSeconds) { var positionInMillis = positionInSeconds * 1000; if (this.previewVoiceRecording) { this.position = positionInMillis; if (_native.NativeHandlers.Audio?.seekToPlayer) { yield _native.NativeHandlers.Audio.seekToPlayer(positionInMillis); } else if (positionInMillis > 0) { this.notifyError('seek-not-supported'); } return; } if (!this.playerRef) { return; } this.position = positionInMillis; if (this.isExpoCLI) { if (this.playerRef?.setPositionAsync) { yield this.playerRef.setPositionAsync(positionInMillis); } else { this.notifyError('seek-not-supported'); } } else { if (this.playerRef?.seek) { this.playerRef.seek(positionInSeconds); } else if (positionInMillis > 0) { this.notifyError('seek-not-supported'); } } }); function seek(_x4) { return _seek.apply(this, arguments); } return seek; }() }, { key: "stop", value: function () { var _stop = (0, _asyncToGenerator2.default)(function* () { yield this.seek(0); this.pause(); }); function stop() { return _stop.apply(this, arguments); } return stop; }() }, { key: "onRemove", value: function onRemove() { this.initRequestId += 1; if (this.previewVoiceRecording) { if (_native.NativeHandlers.Audio?.stopPlayer) { _native.NativeHandlers.Audio.stopPlayer(); } this.state.partialNext({ ...INITIAL_STATE, currentPlaybackRate: this.playbackRates[0], playbackRates: DEFAULT_PLAYBACK_RATES }); return; } if (this.playerRef?.stopAsync) { this.playerRef.stopAsync(); } if (this.playerRef?.unloadAsync) { this.playerRef.unloadAsync(); } this.playerRef = null; this.state.partialNext({ ...INITIAL_STATE, currentPlaybackRate: this.playbackRates[0], playbackRates: DEFAULT_PLAYBACK_RATES }); } }]); }(); //# sourceMappingURL=audio-player.js.map