Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 87a57ec

Browse files
committed
Handle no/blocked microphones in voice messages
Fixes element-hq/element-web#17139
1 parent 8dbcc85 commit 87a57ec

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

src/components/views/rooms/VoiceRecordComposerTile.tsx

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ import LiveRecordingClock from "../voice_messages/LiveRecordingClock";
2727
import {VoiceRecordingStore} from "../../../stores/VoiceRecordingStore";
2828
import {UPDATE_EVENT} from "../../../stores/AsyncStore";
2929
import RecordingPlayback from "../voice_messages/RecordingPlayback";
30+
import Modal from "../../../Modal";
31+
import ErrorDialog from "../dialogs/ErrorDialog";
32+
import CallMediaHandler from "../../../CallMediaHandler";
3033

3134
interface IProps {
3235
room: Room;
@@ -115,16 +118,59 @@ export default class VoiceRecordComposerTile extends React.PureComponent<IProps,
115118
await this.state.recorder.stop();
116119
return;
117120
}
118-
const recorder = VoiceRecordingStore.instance.startRecording();
119-
await recorder.start();
120121

121-
// We don't need to remove the listener: the recorder will clean that up for us.
122-
recorder.on(UPDATE_EVENT, (ev: RecordingState) => {
123-
if (ev === RecordingState.EndingSoon) return; // ignore this state: it has no UI purpose here
124-
this.setState({recordingPhase: ev});
125-
});
122+
// The "microphone access error" dialogs are used a lot, so let's functionify them
123+
const accessError = () => {
124+
Modal.createTrackedDialog('Microphone Access Error', '', ErrorDialog, {
125+
title: _t("Unable to access your microphone"),
126+
description: <>
127+
<p>{_t(
128+
"We were unable to access your microphone. Please check your browser settings and try again.",
129+
)}</p>
130+
</>,
131+
});
132+
};
133+
134+
// Do a sanity test to ensure we're about to grab a valid microphone reference. Things might
135+
// change between this and recording, but at least we will have tried.
136+
try {
137+
const devices = await CallMediaHandler.getDevices();
138+
if (!devices?.['audioinput']?.length) {
139+
Modal.createTrackedDialog('No Microphone Error', '', ErrorDialog, {
140+
title: _t("No microphone found"),
141+
description: <>
142+
<p>{_t(
143+
"We didn't find a microphone on your device. Please check your settings and try again.",
144+
)}</p>
145+
</>,
146+
});
147+
return;
148+
}
149+
// else we probably have a device that is good enough
150+
} catch (e) {
151+
console.error("Error getting devices: ", e);
152+
accessError();
153+
return;
154+
}
155+
156+
try {
157+
const recorder = VoiceRecordingStore.instance.startRecording();
158+
await recorder.start();
159+
160+
// We don't need to remove the listener: the recorder will clean that up for us.
161+
recorder.on(UPDATE_EVENT, (ev: RecordingState) => {
162+
if (ev === RecordingState.EndingSoon) return; // ignore this state: it has no UI purpose here
163+
this.setState({recordingPhase: ev});
164+
});
165+
166+
this.setState({recorder, recordingPhase: RecordingState.Started});
167+
} catch (e) {
168+
console.error("Error starting recording: ", e);
169+
accessError();
126170

127-
this.setState({recorder, recordingPhase: RecordingState.Started});
171+
// noinspection ES6MissingAwait - if this goes wrong we don't want it to affect the call stack
172+
VoiceRecordingStore.instance.disposeRecording();
173+
}
128174
};
129175

130176
private renderWaveformArea(): ReactNode {

src/i18n/strings/en_EN.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,10 @@
16491649
"Invited by %(sender)s": "Invited by %(sender)s",
16501650
"Jump to first unread message.": "Jump to first unread message.",
16511651
"Mark all as read": "Mark all as read",
1652+
"Unable to access your microphone": "Unable to access your microphone",
1653+
"We were unable to access your microphone. Please check your browser settings and try again.": "We were unable to access your microphone. Please check your browser settings and try again.",
1654+
"No microphone found": "No microphone found",
1655+
"We didn't find a microphone on your device. Please check your settings and try again.": "We didn't find a microphone on your device. Please check your settings and try again.",
16521656
"Record a voice message": "Record a voice message",
16531657
"Stop the recording": "Stop the recording",
16541658
"Delete recording": "Delete recording",

0 commit comments

Comments
 (0)