mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 21:06:06 +00:00
send audio as attachment
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { GenericResponse, RecordingData, VoiceRecorder } from 'capacitor-voice-recorder';
|
||||
import { err, ok, Result } from 'neverthrow';
|
||||
|
||||
export enum StartRecordingResultError {
|
||||
NoSpeaker,
|
||||
NeedPermission,
|
||||
alreadyRecording
|
||||
}
|
||||
|
||||
export enum StopRecordingResultError {
|
||||
haventStartYet,
|
||||
NoValue,
|
||||
UnknownError
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class SpeakerService {
|
||||
|
||||
|
||||
recording = false;
|
||||
|
||||
constructor() { }
|
||||
|
||||
|
||||
async startRecording(): Promise<Result<true, StartRecordingResultError>> {
|
||||
// Request audio recording permission
|
||||
await VoiceRecorder.requestAudioRecordingPermission();
|
||||
|
||||
// Check if the device can record audio
|
||||
const canRecord = await VoiceRecorder.canDeviceVoiceRecord();
|
||||
if (!canRecord.value) {
|
||||
return err(StartRecordingResultError.NoSpeaker);
|
||||
}
|
||||
|
||||
// Check if the app has permission to record audio
|
||||
const hasPermission = await VoiceRecorder.requestAudioRecordingPermission();
|
||||
if (!hasPermission.value) {
|
||||
return err(StartRecordingResultError.NeedPermission);
|
||||
}
|
||||
|
||||
// Check if recording is already in progress
|
||||
if (this.recording) {
|
||||
return err(StartRecordingResultError.alreadyRecording)
|
||||
}
|
||||
|
||||
// Start recording
|
||||
this.recording = true;
|
||||
VoiceRecorder.startRecording();
|
||||
return ok(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Example of a poorly structured startRecording method (commented out).
|
||||
*
|
||||
* - This example demonstrates improper chaining of promises and lack of proper error handling.
|
||||
* - Avoid using this approach for better readability and maintainability.
|
||||
*/
|
||||
// bad code example
|
||||
// async startRecording() {
|
||||
// VoiceRecorder.requestAudioRecordingPermission();
|
||||
// if (await VoiceRecorder.canDeviceVoiceRecord().then((result: GenericResponse) => { return result.value })) {
|
||||
// if (await VoiceRecorder.requestAudioRecordingPermission().then((result: GenericResponse) => { return result.value })) {
|
||||
// //if(await this.hasAudioRecordingPermission()){
|
||||
// if (this.recording) {
|
||||
// return;
|
||||
// }
|
||||
// this.recording = true;
|
||||
// VoiceRecorder.startRecording()
|
||||
// //}
|
||||
// }
|
||||
// else {
|
||||
// return err('need Permission');
|
||||
// }
|
||||
// }
|
||||
// else {
|
||||
// return err('no speaker');
|
||||
// }
|
||||
// }
|
||||
|
||||
async stopRecording(): Promise<Result<RecordingData, StopRecordingResultError>> {
|
||||
if (!this.recording) {
|
||||
return err(StopRecordingResultError.haventStartYet);
|
||||
}
|
||||
|
||||
this.recording = false;
|
||||
|
||||
try {
|
||||
const result = await VoiceRecorder.stopRecording();
|
||||
|
||||
if (result.value && result.value.recordDataBase64) {
|
||||
const recordData = result.value.recordDataBase64;
|
||||
return ok(result);
|
||||
} else {
|
||||
return err(StopRecordingResultError.NoValue);
|
||||
}
|
||||
} catch (error) {
|
||||
// Handle any unexpected errors that might occur during stopRecording
|
||||
return err(StopRecordingResultError.UnknownError);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user