48 lines
1.2 KiB
TypeScript
48 lines
1.2 KiB
TypeScript
import { useAudioStore } from "_store/audioStore";
|
|
import {
|
|
LocalTrackPublication,
|
|
RemoteParticipant,
|
|
RemoteTrack,
|
|
RemoteTrackPublication,
|
|
Track,
|
|
} from "livekit-client";
|
|
|
|
export const handleTrackSubscribed = (
|
|
track: RemoteTrack,
|
|
publication: RemoteTrackPublication,
|
|
participant: RemoteParticipant,
|
|
) => {
|
|
if (!track.isMuted) {
|
|
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;
|
|
});
|
|
}
|
|
|
|
track.on("muted", () => {
|
|
useAudioStore.getState().removeSpeakingParticipant(participant);
|
|
});
|
|
};
|
|
|
|
export const handleTrackUnsubscribed = (track: RemoteTrack) => {
|
|
// remove tracks from all attached elements
|
|
track.detach();
|
|
};
|
|
|
|
export const handleLocalTrackUnpublished = (publication: LocalTrackPublication) => {
|
|
// when local tracks are ended, update UI to remove them from rendering
|
|
publication.track?.detach();
|
|
};
|
|
|
|
export const handleDisconnect = () => {
|
|
console.log("disconnected from room");
|
|
};
|