fix: mediaStream not stopping after exiting call mode

This commit is contained in:
Timothy J. Baek 2024-08-22 17:27:22 +02:00
parent 61503a654c
commit 153a2876b4

View File

@ -1,6 +1,6 @@
<script lang="ts"> <script lang="ts">
import { config, models, settings, showCallOverlay } from '$lib/stores'; import { config, models, settings, showCallOverlay } from '$lib/stores';
import { onMount, tick, getContext } from 'svelte'; import { onMount, tick, getContext, onDestroy } from 'svelte';
import { import {
blobToFile, blobToFile,
@ -47,6 +47,7 @@
let rmsLevel = 0; let rmsLevel = 0;
let hasStartedSpeaking = false; let hasStartedSpeaking = false;
let mediaRecorder; let mediaRecorder;
let audioStream = null;
let audioChunks = []; let audioChunks = [];
let videoInputDevices = []; let videoInputDevices = [];
@ -212,17 +213,23 @@
} else { } else {
audioChunks = []; audioChunks = [];
mediaRecorder = false; mediaRecorder = false;
if (audioStream) {
const tracks = audioStream.getTracks();
tracks.forEach((track) => track.stop());
}
audioStream = null;
} }
}; };
const startRecording = async () => { const startRecording = async () => {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); audioStream = await navigator.mediaDevices.getUserMedia({ audio: true });
mediaRecorder = new MediaRecorder(stream); mediaRecorder = new MediaRecorder(audioStream);
mediaRecorder.onstart = () => { mediaRecorder.onstart = () => {
console.log('Recording started'); console.log('Recording started');
audioChunks = []; audioChunks = [];
analyseAudio(stream); analyseAudio(audioStream);
}; };
mediaRecorder.ondataavailable = (event) => { mediaRecorder.ondataavailable = (event) => {
@ -615,6 +622,12 @@
await stopCamera(); await stopCamera();
}; };
}); });
onDestroy(async () => {
await stopAllAudio();
await stopRecordingCallback(false);
await stopCamera();
});
</script> </script>
{#if $showCallOverlay} {#if $showCallOverlay}