aloof-scooter-47164
10/04/2025, 11:13 PMtall-airline-16756
10/04/2025, 11:13 PMtall-airline-16756
10/04/2025, 11:13 PMPlaybackFinishedEvent emitted by the AudioOutput object. This event carries a handy playback_position field, which tells you the total duration of audio played back in seconds.
To get the number of samples, simply multiply the playback_position by the sample_rate from the AudioOutput configuration. Here's a little example to guide you:
from livekit.agents.voice.io import PlaybackFinishedEvent
@session.output.audio.on("playback_finished")
def _on_playback_finished(ev: PlaybackFinishedEvent):
sample_rate = session.output.audio.sample_rate # Final output sample rate
playback_position = ev.playback_position # Duration in seconds
if sample_rate is not None:
total_samples = int(playback_position * sample_rate)
print(f"Number of samples sent: {total_samples}")
This method helps ensure you get an accurate count of the total samples sent, even if there’s any intermediate resampling during processing. If you have any questions or need further help with this, feel free to ask—I'm here to support you!
Sources: Bringing AI avatars to voice agents | livekit-agents/livekit/agents/voice/transcription/_speaking_rate.pyaloof-scooter-47164
10/04/2025, 11:26 PMtall-airline-16756
10/04/2025, 11:27 PMonFirstFrame callback. This event is triggered as soon as the very first audio frame is captured and sent, which means audio output has just begun.
Here’s a simple example to illustrate how you might implement it:
def _on_first_frame(future: asyncio.Future[None]) -> None:
self._session._update_agent_state("speaking")
print("Agent started sending audio")
Usually, this callback is connected to the audio output process during tasks like TTS or real-time audio generation. When the first audio frame gets processed, the callback activates, letting you know that audio transmission is underway.
If you’d like any more help setting this up or have other questions, please don’t hesitate to reach out—I’m here to assist you!
Sources: agents/src/voice/agent_activity.ts | How to detect when an agent has finished speaking | examples/avatar_agents/bey/README.md