From 710fde6a6faa6dc69b57bd79ce10a006ade35c20 Mon Sep 17 00:00:00 2001 From: Marco Beretta <81851188+berry-13@users.noreply.github.com> Date: Fri, 4 Apr 2025 17:56:47 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=84=20fix:=20Improve=20audio=20MIME=20?= =?UTF-8?q?type=20detection=20and=20handling=20in=20Speech=20to=20Text=20h?= =?UTF-8?q?ook=20(#6707)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hooks/Input/useSpeechToTextExternal.ts | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/client/src/hooks/Input/useSpeechToTextExternal.ts b/client/src/hooks/Input/useSpeechToTextExternal.ts index 5ddccc9f3e..3160696e9c 100644 --- a/client/src/hooks/Input/useSpeechToTextExternal.ts +++ b/client/src/hooks/Input/useSpeechToTextExternal.ts @@ -21,7 +21,7 @@ const useSpeechToTextExternal = ( const [isListening, setIsListening] = useState(false); const [audioChunks, setAudioChunks] = useState([]); const [isRequestBeingMade, setIsRequestBeingMade] = useState(false); - const [audioMimeType, setAudioMimeType] = useState('audio/webm'); + const [audioMimeType, setAudioMimeType] = useState(() => getBestSupportedMimeType()); const [minDecibels] = useRecoilState(store.decibelValue); const [autoSendText] = useRecoilState(store.autoSendText); @@ -49,7 +49,7 @@ const useSpeechToTextExternal = ( }, }); - const getBestSupportedMimeType = () => { + function getBestSupportedMimeType() { const types = [ 'audio/webm', 'audio/webm;codecs=opus', @@ -60,20 +60,22 @@ const useSpeechToTextExternal = ( ]; for (const type of types) { - if (MediaRecorder.isTypeSupported(type)) { + if (typeof MediaRecorder !== 'undefined' && MediaRecorder.isTypeSupported(type)) { return type; } } - const ua = navigator.userAgent.toLowerCase(); - if (ua.indexOf('safari') !== -1 && ua.indexOf('chrome') === -1) { - return 'audio/mp4'; - } else if (ua.indexOf('firefox') !== -1) { - return 'audio/ogg'; - } else { - return 'audio/webm'; + if (typeof navigator !== 'undefined') { + const ua = navigator.userAgent.toLowerCase(); + if (ua.indexOf('safari') !== -1 && ua.indexOf('chrome') === -1) { + return 'audio/mp4'; + } else if (ua.indexOf('firefox') !== -1) { + return 'audio/ogg'; + } } - }; + + return 'audio/webm'; + } const getFileExtension = (mimeType: string) => { if (mimeType.includes('mp4')) { @@ -177,7 +179,7 @@ const useSpeechToTextExternal = ( setAudioMimeType(bestMimeType); mediaRecorderRef.current = new MediaRecorder(audioStream.current, { - mimeType: bestMimeType, + mimeType: audioMimeType, }); mediaRecorderRef.current.addEventListener('dataavailable', (event: BlobEvent) => { audioChunks.push(event.data); @@ -266,7 +268,6 @@ const useSpeechToTextExternal = ( return () => { window.removeEventListener('keydown', handleKeyDown); }; - }, [isListening]); return {