Subscribe to Media Tracks
Render the agent's video and audio in your app
The agent publishes its rendered output as two separate LiveKit media tracks: one video track for the avatar and one audio track for its voice. To play the agent, listen for TrackSubscribed and attach each track to a media sink.
Note: Attach the listener before calling
room.connect()so you don't miss the initial subscription events.
Subscribing to tracks
import { RoomEvent, Track } from "livekit-client";
room.on(RoomEvent.TrackSubscribed, (track) => {
if (track.kind === Track.Kind.Video) {
track.attach(document.getElementById("agent-video") as HTMLVideoElement);
} else if (track.kind === Track.Kind.Audio) {
track.attach(document.getElementById("agent-audio") as HTMLAudioElement);
}
});Your HTML needs a <video> and <audio> element for the tracks to attach to:
<video id="agent-video" autoplay playsinline></video>
<audio id="agent-audio" autoplay></audio>from livekit import rtc
def on_track_subscribed(track, publication, participant):
if track.kind == rtc.TrackKind.KIND_VIDEO:
# Handle video frames (e.g. forward to a renderer or recorder)
pass
elif track.kind == rtc.TrackKind.KIND_AUDIO:
# Handle audio frames (e.g. forward to a speaker or recorder)
pass
room.on("track_subscribed", on_track_subscribed)Python rendering depends on your runtime — see the LiveKit Python tracks guide for frame handling examples.
room.listener = object : RoomListener {
override fun onTrackSubscribed(
track: Track,
publication: RemoteTrackPublication,
participant: RemoteParticipant
) {
when (track) {
is VideoTrack -> {
// Attach to a VideoView in your UI
videoView.addRenderer(track)
}
is AudioTrack -> {
// Audio plays through the default output by default
}
}
}
}Updated about 2 months ago
What’s Next
Did this page help you?
