From 41a3086d82e5eefb21fd7b5122b1ad44f357533c Mon Sep 17 00:00:00 2001 From: PxlLoewe <72106766+PxlLoewe@users.noreply.github.com> Date: Fri, 18 Jul 2025 18:21:39 -0700 Subject: [PATCH] fix Livekit Audio --- .../app/_helpers/liveKitEventHandler.ts | 34 ++++++++++++------- apps/dispatch/app/_store/audioStore.ts | 4 ++- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/apps/dispatch/app/_helpers/liveKitEventHandler.ts b/apps/dispatch/app/_helpers/liveKitEventHandler.ts index b26f6f1a..bfc4ead3 100644 --- a/apps/dispatch/app/_helpers/liveKitEventHandler.ts +++ b/apps/dispatch/app/_helpers/liveKitEventHandler.ts @@ -4,30 +4,38 @@ import { RemoteParticipant, RemoteTrack, RemoteTrackPublication, - Track, } from "livekit-client"; +const initialTrackTimeouts = new Map(); + export const handleTrackSubscribed = ( track: RemoteTrack, publication: RemoteTrackPublication, participant: RemoteParticipant, ) => { + const element = track.attach(); + element.pause(); + if (!track.isMuted) { + initialTrackTimeouts.set( + participant.sid, + setTimeout(() => { + useAudioStore.getState().addSpeakingParticipant(participant); + }, 1000), + ); + setTimeout(() => { + element.play(); + }, 1000); + } + + track.on("unmuted", () => { useAudioStore.getState().addSpeakingParticipant(participant); - } - - if (track.kind === Track.Kind.Video || track.kind === Track.Kind.Audio) { - // attach it to a new HTMLVideoElement or HTMLAudioElement - const element = track.attach(); - element.play(); - - track.on("unmuted", () => { - useAudioStore.getState().addSpeakingParticipant(participant); - element.volume = useAudioStore.getState().settings.radioVolume; - }); - } + element.volume = useAudioStore.getState().settings.radioVolume; + }); track.on("muted", () => { + clearTimeout(initialTrackTimeouts.get(participant.sid)); + initialTrackTimeouts.get(participant.sid); useAudioStore.getState().removeSpeakingParticipant(participant); }); }; diff --git a/apps/dispatch/app/_store/audioStore.ts b/apps/dispatch/app/_store/audioStore.ts index bed83a18..76648136 100644 --- a/apps/dispatch/app/_store/audioStore.ts +++ b/apps/dispatch/app/_store/audioStore.ts @@ -184,7 +184,9 @@ export const useAudioStore = create((set, get) => ({ name: "radio-audio", source: Track.Source.Microphone, }); - await publishedTrack.mute(); + setTimeout(() => { + publishedTrack.mute(); + }, 400); set({ localRadioTrack: publishedTrack }); set({ state: "connected", room, message: null });