is there an event that specifies when the user sst...
# ask-ai
b
is there an event that specifies when the user sstopped speaking? and when the agent started speaking?
t
Thanks for reaching out, Sebastian. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to help you understand how LiveKit handles events when a user stops speaking and another user, like an agent, starts speaking. Depending on the version of the LiveKit Agents framework you're using, the events vary a bit: • For LiveKit Agents v0.x: You’ll want to look out for the events
user_started_speaking
,
user_stopped_speaking
,
agent_started_speaking
, and
agent_stopped_speaking
. These are triggered through voice activity detection (VAD), which helps track when someone is talking. • For LiveKit Agents v1.x: The events have been updated to
user_state_changed
and
agent_state_changed
. In these, the
new_state
field tells you if someone has switched to states like
speaking
or
listening
. If you'd like, I’d be more than happy to provide more details or some examples on how to implement these events. Just let me know! Sources: Agents 0.x migration guide | LiveKit Docs | Agents v0.x migration guide - Node.js | LiveKit Docs | Events and error handling | LiveKit Docs | VoicePipelineAgent | LiveKit Docs | Turn detection and interruptions | LiveKit Docs
b
give me example code
t
I'd love to share some example code snippets to help you handle speaking state events in LiveKit. Let's walk through them together: For LiveKit Agents v0.x:
Copy code
@agent.on("user_started_speaking")
def user_started_speaking():
    print("User started speaking")

@agent.on("user_stopped_speaking")
def user_stopped_speaking():
    print("User stopped speaking")

@agent.on("agent_started_speaking")
def agent_started_speaking():
    print("Agent started speaking")

@agent.on("agent_stopped_speaking")
def agent_stopped_speaking():
    print("Agent stopped speaking")
For LiveKit Agents v1.x:
Copy code
@session.on("user_state_changed")
def on_user_state_changed(ev):
    if ev.new_state == "speaking":
        print("User started speaking")
    elif ev.new_state == "listening":
        print("User stopped speaking")

@session.on("agent_state_changed")
def on_agent_state_changed(ev):
    if ev.new_state == "speaking":
        print("Agent started speaking")
If you have any questions or need a bit more explanation, please don’t hesitate to ask—I’m here to help!