Skip to content

Commit

Permalink
Release microphone immediately when cancelling recording
Browse files Browse the repository at this point in the history
Co-authored-by: Josh Perez <60019601+josh-signal@users.noreply.github.com>
  • Loading branch information
automated-signal and josh-signal committed Oct 15, 2021
1 parent 8c94e99 commit 827dd96
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
7 changes: 7 additions & 0 deletions ts/services/audioRecorder.ts
Expand Up @@ -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;

Expand All @@ -33,6 +34,7 @@ export class RecorderClass {
}

this.input = undefined;
this.stream = undefined;

if (this.context) {
this.context.close();
Expand Down Expand Up @@ -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:',
Expand All @@ -87,6 +90,10 @@ export class RecorderClass {
return;
}

if (this.stream) {
this.stream.getTracks().forEach(track => track.stop());
}

if (this.blob) {
return this.blob;
}
Expand Down
19 changes: 13 additions & 6 deletions ts/state/ducks/audioRecorder.ts
Expand Up @@ -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,
});
};
}

Expand Down Expand Up @@ -200,7 +208,6 @@ export function reducer(
return {
...state,
errorDialogAudioRecorderType: action.payload,
isRecording: false,
};
}

Expand Down

0 comments on commit 827dd96

Please sign in to comment.