UNPKG

@independo/capacitor-voice-recorder

Version:
60 lines 2.28 kB
import { attachCanonicalErrorCode } from '../core/error-codes'; import { normalizeRecordingData } from '../core/recording-contract'; /** Orchestrates platform calls and normalizes responses when requested. */ export class VoiceRecorderService { constructor(platform, responseFormat) { this.platform = platform; this.responseFormat = responseFormat; } /** Checks whether the device can record audio. */ canDeviceVoiceRecord() { return this.execute(() => this.platform.canDeviceVoiceRecord()); } /** Returns whether microphone permission is currently granted. */ hasAudioRecordingPermission() { return this.execute(() => this.platform.hasAudioRecordingPermission()); } /** Requests microphone permission from the user. */ requestAudioRecordingPermission() { return this.execute(() => this.platform.requestAudioRecordingPermission()); } /** Starts a recording session. */ startRecording(options) { return this.execute(() => this.platform.startRecording(options)); } /** Stops the recording session and formats the payload if needed. */ async stopRecording() { return this.execute(async () => { const data = await this.platform.stopRecording(); if (this.responseFormat === 'normalized') { return normalizeRecordingData(data); } return data; }); } /** Pauses the recording session when supported. */ pauseRecording() { return this.execute(() => this.platform.pauseRecording()); } /** Resumes a paused recording session when supported. */ resumeRecording() { return this.execute(() => this.platform.resumeRecording()); } /** Returns the current recording state. */ getCurrentStatus() { return this.execute(() => this.platform.getCurrentStatus()); } /** Wraps calls to apply canonical error codes when requested. */ async execute(fn) { try { return await fn(); } catch (error) { if (this.responseFormat === 'normalized') { attachCanonicalErrorCode(error); } throw error; } } } //# sourceMappingURL=VoiceRecorderService.js.map