Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added recorder state emitter #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/lib/ng-audio-recorder.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class NgAudioRecorderService {
private chunks: Array<any> = [];
protected recorderEnded = new EventEmitter();
public recorderError = new EventEmitter<ErrorCase>();
public recorderState = new EventEmitter<RecorderState>();
// tslint:disable-next-line
private _recorderState = RecorderState.INITIALIZING;

Expand Down Expand Up @@ -38,22 +39,26 @@ 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)
});
}

pause() {
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();
}
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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 = [];
Expand All @@ -105,7 +112,6 @@ export class NgAudioRecorderService {
}
}


export enum OutputFormat {
WEBM_BLOB_URL,
WEBM_BLOB,
Expand Down