diff --git a/ts/services/audioRecorder.ts b/ts/services/audioRecorder.ts index d6c29169e..9d399cab0 100644 --- a/ts/services/audioRecorder.ts +++ b/ts/services/audioRecorder.ts @@ -9,6 +9,7 @@ export class RecorderClass { private input?: GainNode; private recorder?: WebAudioRecorderClass; private source?: MediaStreamAudioSourceNode; + private stream?: MediaStream; private blob?: Blob; private resolve?: (blob: Blob) => void; @@ -33,6 +34,7 @@ export class RecorderClass { } this.input = undefined; + this.stream = undefined; if (this.context) { this.context.close(); @@ -67,6 +69,7 @@ export class RecorderClass { } this.source = this.context.createMediaStreamSource(stream); this.source.connect(this.input); + this.stream = stream; } catch (err) { log.error( 'Recorder.onGetUserMediaError:', @@ -87,6 +90,10 @@ export class RecorderClass { return; } + if (this.stream) { + this.stream.getTracks().forEach(track => track.stop()); + } + if (this.blob) { return this.blob; } diff --git a/ts/state/ducks/audioRecorder.ts b/ts/state/ducks/audioRecorder.ts index 73b0eaaad..71cac4ff6 100644 --- a/ts/state/ducks/audioRecorder.ts +++ b/ts/state/ducks/audioRecorder.ts @@ -148,12 +148,20 @@ function completeRecording( }; } -function cancelRecording(): CancelRecordingAction { - recorder.clear(); +function cancelRecording(): ThunkAction< + void, + RootStateType, + unknown, + CancelRecordingAction +> { + return async dispatch => { + await recorder.stop(); + recorder.clear(); - return { - type: CANCEL_RECORDING, - payload: undefined, + dispatch({ + type: CANCEL_RECORDING, + payload: undefined, + }); }; } @@ -200,7 +208,6 @@ export function reducer( return { ...state, errorDialogAudioRecorderType: action.payload, - isRecording: false, }; }