浏览代码

fix: voice recording not stopping

Timothy J. Baek 7 月之前
父节点
当前提交
cb81dfe4ba
共有 1 个文件被更改,包括 9 次插入1 次删除
  1. 9 1
      src/lib/components/chat/MessageInput/VoiceRecording.svelte

+ 9 - 1
src/lib/components/chat/MessageInput/VoiceRecording.svelte

@@ -44,6 +44,7 @@
 		return `${minutes}:${formattedSeconds}`;
 		return `${minutes}:${formattedSeconds}`;
 	};
 	};
 
 
+	let stream;
 	let speechRecognition;
 	let speechRecognition;
 
 
 	let mediaRecorder;
 	let mediaRecorder;
@@ -159,7 +160,7 @@
 	const startRecording = async () => {
 	const startRecording = async () => {
 		startDurationCounter();
 		startDurationCounter();
 
 
-		const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
+		stream = await navigator.mediaDevices.getUserMedia({ audio: true });
 		mediaRecorder = new MediaRecorder(stream);
 		mediaRecorder = new MediaRecorder(stream);
 		mediaRecorder.onstart = () => {
 		mediaRecorder.onstart = () => {
 			console.log('Recording started');
 			console.log('Recording started');
@@ -251,6 +252,13 @@
 		}
 		}
 		stopDurationCounter();
 		stopDurationCounter();
 		audioChunks = [];
 		audioChunks = [];
+
+		if (stream) {
+			const tracks = stream.getTracks();
+			tracks.forEach((track) => track.stop());
+		}
+
+		stream = null;
 	};
 	};
 
 
 	const confirmRecording = async () => {
 	const confirmRecording = async () => {