I need my agent to say some warning if the user ha...
# ask-ai
f
I need my agent to say some warning if the user has switched off the microphone or video. How can i do this?
t
Thanks for reaching out, Alexander. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to help you detect when a user has switched off their microphone or video by monitoring track mute/unmute events in LiveKit. Here’s a friendly guide on how you can set this up: Event-Based Detection: • Keep an eye out for
TrackMuted
and
TrackUnmuted
events to know exactly when a track gets muted or unmuted.
Copy code
room.on(RoomEvent.TrackMuted, (publication, participant) => {
  if (participant === room.localParticipant) {
    if (publication.source === Track.Source.Microphone) {
      triggerMicrophoneWarning();
    } else if (publication.source === Track.Source.Camera) {
      triggerCameraWarning();
    }
  }
});

room.on(RoomEvent.TrackUnmuted, (publication, participant) => {
  if (participant === room.localParticipant) {
    clearWarnings();
  }
});
Checking Mute Status: • You can also check the current mute status anytime by using the
isMuted
property on track publications.
Copy code
const micPub = room.localParticipant.getTrackPublication(Track.Source.Microphone);
const micEnabled = micPub && micPub.isSubscribed && !micPub.isMuted;

if (!micEnabled) {
  triggerMicrophoneWarning();
}
These approaches should help you gently notify users when their microphone or video is off. If you have any questions or need a hand with implementation, just let me know—I’m here to support you! Sources: src/room/events.ts | complex-agents/turn-taking/turn-taking-frontend/app/page.tsx | Samples~/JSExample/JSPackage~/src/index.ts | video-encode-decode-test/src/main/java/io/livekit/android/videoencodedecode... | README.md
f
i did this event based logic. and used session.say() to make the agent say a message. but this didnt come up. Why would that be the case?
t
I'm sorry to hear you're having trouble with
session.say()
not working. Let's walk through some common reasons this might happen, so we can get it sorted out together: 1. Session State: It’s important to make sure your session is active when you call
session.say()
. If the session isn’t running or is closing, this method won’t work as expected. 2. Missing TTS Plugin: Double-check that your agent has a Text-to-Speech (TTS) plugin configured. Without this,
session.say()
won’t function properly. 3. Event Timing: Sometimes, if the event triggering
session.say()
happens after the session has ended or is outside the right scope, the method can fail. Make sure the event fires while the session is still active. 4. Background Tasks: Try to avoid calling
session.say()
from asynchronous tasks that might continue after the session lifecycle ends. To help you debug, you might want to: • Keep an eye on the session state using the
agent_state_changed
event to confirm it’s in an active state like “listening” or “thinking.” • Check if the
speech_created
event fires when you call
session.say()
. If you’d like, please feel free to share more details about your setup or any specific errors you’re seeing—I’m here to help you get this working smoothly! Sources: pipeline-tts/tts_node.py | Events and error handling | LiveKit Docs | agents/src/voice/agent_activity.ts | livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti... | Agent speech and audio | LiveKit Docs
f
ok tell me how i have to register the event handler for the room events? should i do it inside the agent class? or should it be done inside the session class?
t
Hi there! When working with room event handlers, it's best to register them on the
rtc.Room
object. A great approach is to handle this within your agent class or a dedicated manager class like
RoomIO
. This helps keep everything nicely organized and ensures your room-level events are managed properly. For instance, in your agent class, it might look like this:
Copy code
class MyAgent:
    def __init__(self, ctx: agents.JobContext):
        self._ctx = ctx

    async def connect(self):
        await self._ctx.connect()
        self._ctx.room.on("participant_connected", self._on_participant_connected)
        self._ctx.room.on("participant_disconnected", self._on_participant_disconnected)
Or, if you prefer using a manager class like
RoomIO
, you could do it this way:
Copy code
class RoomIO:
    def __init__(self, agent_session: AgentSession, room: rtc.Room):
        self._room = room

    async def start(self):
        self._room.on("participant_connected", self._on_participant_connected)
        self._room.on("connection_state_changed", self._on_connection_state_changed)
Just a quick note: it's best to avoid registering room events inside the session class, since the session is really meant for session-specific events like
agent_state_changed
. If any of this feels unclear or if you'd like me to walk you through it further, please don't hesitate to ask—I’m here to help! Sources: agent/game_host.py | livekit-agents/livekit/agents/voice/room_io/room_io.py | agent.py | agents/src/voice/room_io/room_io.ts