UNPKG

stream-chat-react-native-core

Version:

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

152 lines 4.77 kB
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.AudioRecorderManager = void 0; var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _reactNative = require("react-native"); var _streamChat = require("stream-chat"); var _normalizeAudioLevel = require("../components/MessageInput/utils/normalizeAudioLevel"); var _native = require("../native"); var INITIAL_STATE = { isStarting: false, micLocked: false, waveformData: [], recording: undefined, duration: 0, status: 'idle' }; class AudioRecorderManager { pendingStopRequestIds = new Set(); startRequestId = 0; constructor() { this.state = new _streamChat.StateStore(INITIAL_STATE); } reset() { this.state.next(INITIAL_STATE); } onRecordingStatusUpdate = status => { if (status.isDoneRecording === true) { return; } this.state.partialNext({ duration: (status == null ? void 0 : status.currentPosition) || status.durationMillis }); var lowerBound = _reactNative.Platform.OS === 'ios' || status.currentMetering ? -60 : -120; var normalizedAudioLevel = (0, _normalizeAudioLevel.normalizeAudioLevel)(status.currentMetering || status.metering, lowerBound); this.state.partialNext({ waveformData: [...this.state.getLatestValue().waveformData, normalizedAudioLevel] }); }; startRecording() { var _this = this; return (0, _asyncToGenerator2.default)(function* () { if (!_native.NativeHandlers.Audio) { return; } var _this$state$getLatest = _this.state.getLatestValue(), isStarting = _this$state$getLatest.isStarting, status = _this$state$getLatest.status; if (isStarting || status === 'recording') { return true; } var requestId = ++_this.startRequestId; _this.state.partialNext({ duration: 0, isStarting: true, micLocked: false, recording: undefined, status: 'idle', waveformData: [] }); var recordingInfo; try { recordingInfo = yield _native.NativeHandlers.Audio.startRecording({ isMeteringEnabled: true }, _this.onRecordingStatusUpdate); } catch (_unused) { _this.pendingStopRequestIds.delete(requestId); if (requestId === _this.startRequestId) { _this.reset(); } return false; } var _recordingInfo = recordingInfo, accessGranted = _recordingInfo.accessGranted, recording = _recordingInfo.recording; if (_this.pendingStopRequestIds.has(requestId)) { if (accessGranted) { try { yield _native.NativeHandlers.Audio.stopRecording(); } catch (_unused2) {} } _this.pendingStopRequestIds.delete(requestId); if (requestId === _this.startRequestId) { _this.reset(); } return accessGranted; } if (requestId !== _this.startRequestId) { return accessGranted; } if (accessGranted && recording) { if (recording && typeof recording !== 'string' && recording.setProgressUpdateInterval) { recording.setProgressUpdateInterval(_reactNative.Platform.OS === 'android' ? 100 : 60); } _this.state.partialNext({ isStarting: false, recording, status: 'recording' }); } else { _this.reset(); } return accessGranted; })(); } stopRecording() { var _this2 = this; return (0, _asyncToGenerator2.default)(function* (withDelete = false) { if (!_native.NativeHandlers.Audio) { return; } var _this2$state$getLates = _this2.state.getLatestValue(), isStarting = _this2$state$getLates.isStarting, status = _this2$state$getLates.status; if (isStarting) { _this2.pendingStopRequestIds.add(_this2.startRequestId); _this2.reset(); return; } if (status !== 'recording') { if (withDelete) { _this2.reset(); } return; } try { yield _native.NativeHandlers.Audio.stopRecording(); } catch (_unused3) {} if (withDelete) { _this2.reset(); } else { _this2.state.partialNext({ isStarting: false, status: 'stopped' }); } }).apply(this, arguments); } set micLocked(value) { this.state.partialNext({ micLocked: value }); } set status(value) { this.state.partialNext({ status: value }); } } exports.AudioRecorderManager = AudioRecorderManager; //# sourceMappingURL=audio-recorder-manager.js.map