From 1efb3ca960397bfce1a28aceefa3c1be4f85a142 Mon Sep 17 00:00:00 2001 From: Lucas Alderete Date: Sat, 19 Feb 2022 23:21:06 -0300 Subject: [PATCH] Added recorder state emitter Added recorderState emitter for subscribers when change state --- src/lib/ng-audio-recorder.service.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/lib/ng-audio-recorder.service.ts b/src/lib/ng-audio-recorder.service.ts index 3ec8b92..cb43419 100644 --- a/src/lib/ng-audio-recorder.service.ts +++ b/src/lib/ng-audio-recorder.service.ts @@ -8,6 +8,7 @@ export class NgAudioRecorderService { private chunks: Array = []; protected recorderEnded = new EventEmitter(); public recorderError = new EventEmitter(); + public recorderState = new EventEmitter(); // tslint:disable-next-line private _recorderState = RecorderState.INITIALIZING; @@ -38,9 +39,11 @@ export class NgAudioRecorderService { NgAudioRecorderService.guc().then((mediaStream) => { this.recorder = new MediaRecorder(mediaStream); this._recorderState = RecorderState.INITIALIZED; + this.recorderState.emit(this._recorderState) this.addListeners(); this.recorder.start(); this._recorderState = RecorderState.RECORDING; + this.recorderState.emit(this._recorderState) }); } @@ -48,12 +51,14 @@ export class NgAudioRecorderService { if (this._recorderState === RecorderState.RECORDING) { this.recorder.pause(); this._recorderState = RecorderState.PAUSED; + this.recorderState.emit(this._recorderState) } } resume() { if (this._recorderState === RecorderState.PAUSED) { this._recorderState = RecorderState.RECORDING; + this.recorderState.emit(this._recorderState) this.recorder.resume(); } } @@ -63,6 +68,7 @@ export class NgAudioRecorderService { return new Promise((resolve, reject) => { this.recorderEnded.subscribe((blob) => { this._recorderState = RecorderState.STOPPED; + this.recorderState.emit(this._recorderState) if (outputFormat === OutputFormat.WEBM_BLOB) { resolve(blob); } @@ -92,6 +98,7 @@ export class NgAudioRecorderService { private appendToChunks = (event: any) => { this.chunks.push(event.data); }; + private recordingStopped = (event: any) => { const blob = new Blob(this.chunks, {type: 'audio/webm'}); this.chunks = []; @@ -105,7 +112,6 @@ export class NgAudioRecorderService { } } - export enum OutputFormat { WEBM_BLOB_URL, WEBM_BLOB,