fix Livekit Audio
This commit is contained in:
@@ -4,30 +4,38 @@ import {
|
|||||||
RemoteParticipant,
|
RemoteParticipant,
|
||||||
RemoteTrack,
|
RemoteTrack,
|
||||||
RemoteTrackPublication,
|
RemoteTrackPublication,
|
||||||
Track,
|
|
||||||
} from "livekit-client";
|
} from "livekit-client";
|
||||||
|
|
||||||
|
const initialTrackTimeouts = new Map<string, NodeJS.Timeout>();
|
||||||
|
|
||||||
export const handleTrackSubscribed = (
|
export const handleTrackSubscribed = (
|
||||||
track: RemoteTrack,
|
track: RemoteTrack,
|
||||||
publication: RemoteTrackPublication,
|
publication: RemoteTrackPublication,
|
||||||
participant: RemoteParticipant,
|
participant: RemoteParticipant,
|
||||||
) => {
|
) => {
|
||||||
|
const element = track.attach();
|
||||||
|
element.pause();
|
||||||
|
|
||||||
if (!track.isMuted) {
|
if (!track.isMuted) {
|
||||||
|
initialTrackTimeouts.set(
|
||||||
|
participant.sid,
|
||||||
|
setTimeout(() => {
|
||||||
|
useAudioStore.getState().addSpeakingParticipant(participant);
|
||||||
|
}, 1000),
|
||||||
|
);
|
||||||
|
setTimeout(() => {
|
||||||
|
element.play();
|
||||||
|
}, 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
track.on("unmuted", () => {
|
||||||
useAudioStore.getState().addSpeakingParticipant(participant);
|
useAudioStore.getState().addSpeakingParticipant(participant);
|
||||||
}
|
element.volume = useAudioStore.getState().settings.radioVolume;
|
||||||
|
});
|
||||||
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;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
track.on("muted", () => {
|
track.on("muted", () => {
|
||||||
|
clearTimeout(initialTrackTimeouts.get(participant.sid));
|
||||||
|
initialTrackTimeouts.get(participant.sid);
|
||||||
useAudioStore.getState().removeSpeakingParticipant(participant);
|
useAudioStore.getState().removeSpeakingParticipant(participant);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -184,7 +184,9 @@ export const useAudioStore = create<TalkState>((set, get) => ({
|
|||||||
name: "radio-audio",
|
name: "radio-audio",
|
||||||
source: Track.Source.Microphone,
|
source: Track.Source.Microphone,
|
||||||
});
|
});
|
||||||
await publishedTrack.mute();
|
setTimeout(() => {
|
||||||
|
publishedTrack.mute();
|
||||||
|
}, 400);
|
||||||
set({ localRadioTrack: publishedTrack });
|
set({ localRadioTrack: publishedTrack });
|
||||||
|
|
||||||
set({ state: "connected", room, message: null });
|
set({ state: "connected", room, message: null });
|
||||||
|
|||||||
Reference in New Issue
Block a user