tall-autumn-57974
08/18/2025, 9:06 AMtall-airline-16756
08/18/2025, 9:06 AMtall-airline-16756
08/18/2025, 9:06 AMtall-autumn-57974
08/18/2025, 9:08 AMtall-airline-16756
08/18/2025, 9:08 AMon_enter
Lifecycle Hook: This lets you play a warm welcome message right when the agent becomes active in the session. For example:
from livekit.agents import Agent, AgentSession
from livekit.plugins import openai
class WelcomeAgent(Agent):
def __init__(self):
super().__init__(
instructions="A friendly agent that provides customer support.",
tts=openai.TTS()
)
async def on_enter(self):
self.session.say("Welcome! Your call will be handled by our voice bot.")
async def entrypoint(ctx):
session = AgentSession()
await session.start(
agent=WelcomeAgent(),
room=ctx.room
)
• Use Events: Another cool option is to trigger the welcome audio with the participant_joined
event whenever someone joins the room.
• Background Audio: And after the welcome message, you can keep the vibe going by using BackgroundAudioPlayer
to play some ambient sounds during the session.
If you want me to walk you through any part of this or need a hand setting it up, just let me know—I’m here to help!tall-autumn-57974
08/18/2025, 9:09 AMtall-airline-16756
08/18/2025, 9:10 AMsession.say()
method. This method takes an audio
parameter of type AsyncIterable[rtc.AudioFrame]
. Here’s a quick example for you:
async def audio_generator():
yield audio_frame # audio_frame should be an rtc.AudioFrame object
await session.say("Message", audio=audio_generator())
That said, the details on how to convert an MP3 file into rtc.AudioFrame
objects aren’t covered here. You’ll probably need to handle that conversion yourself before passing it in. If you want, I’m happy to help you figure that part out—just let me know!
Sources: basics/playing_audio.py | Agent speech and audio | LiveKit Docstall-autumn-57974
08/18/2025, 9:15 AMAgentSession
tall-airline-16756
08/18/2025, 9:15 AMrefined-appointment-81829
08/18/2025, 12:34 PMBackgroundAudioPlayer
to play your welcome audio. You can do this by following these steps:
1. Create an instance of BackgroundAudioPlayer
.
2. Play the welcome audio using the play
method.
3. Wait for the audio to finish before starting the agent session.
4. Start the agent session as usual.
Here's an example of how you can implement this in Python:
from livekit.agents import BackgroundAudioPlayer, AudioConfig
import asyncio
async def main(ctx):
# Create the background audio player
background_audio = BackgroundAudioPlayer()
# Start the background audio player
await background_audio.start(room=ctx.room, agent_session=session)
# Play the welcome audio
play_handle = background_audio.play("/path/to/welcome-audio.mp3")
# Wait for the audio to finish playing
await play_handle
# Now start the agent session
session = AgentSession(
stt=deepgram.STT(),
llm=openai.LLM(),
tts=cartesia.TTS(),
vad=silero.VAD.load(),
turn_detection=turn_detector.MultilingualModel(),
)
await session.start(
room=ctx.room,
agent=Agent(instructions="You are a helpful voice AI assistant."),
room_input_options=RoomInputOptions(
noise_cancellation=noise_cancellation.BVC(),
),
)
# Optionally, you can also play background audio during the session
await background_audio.play("/path/to/background-audio.mp3", loop=True)
# Run the main function
asyncio.run(main(ctx))
Explanation:
• Playing the Audio: The play
method is called with the path to your welcome audio file (e.g., an MP3 file). This method returns a PlayHandle
, which you can await to ensure the audio finishes playing before proceeding.
• Starting the Agent Session: After the welcome audio has finished playing, you can start the agent session as you normally would.
Publishing an Audio Track Directly
If you prefer to publish an audio track directly to the room instead of using the BackgroundAudioPlayer
, you can use the LiveKit Python SDK to create an audio track and publish it. Here’s a brief example:
from livekit import Room, AudioTrack
async def publish_audio_track(room: Room):
# Create an audio track from an audio file
audio_track = AudioTrack("/path/to/welcome-audio.mp3")
# Publish the audio track to the room
await room.publish_track(audio_track)
# Call this function before starting the agent session
await publish_audio_track(ctx.room)
Notes:
• Ensure that the audio file paths are correct and accessible.
• You can use either TTS or pre-recorded audio files (like MP3s) based on your preference. The above examples demonstrate how to use an MP3 file for the welcome message.
This approach allows you to provide a seamless experience for users entering the room while ensuring that the agent session starts only after the welcome audio has been played.tall-autumn-57974
08/18/2025, 2:09 PMBackgroundAudioPlayer
needs a session
object to be able to run
Exception: cannot access local participant before connecting {"pid": 3323, "job_id": "AJ_G8VBQnBxBteF"}
However, if I run this code after starting the session, it will be in competition with my call background audio and won't be played. The agent will start speaking instantly
2. For the audio track direct publish, I found the method room.local_participant.publish_track
however I'm not sure to understand how I can transform a remote mp3 url (from s3 for instance) into an AudioTrack
object 🤔 Semms like AudioTrack
takes an AudioSource
but none of them seem to have a param audio_file_path
or audio_file_url
refined-appointment-81829
08/18/2025, 2:36 PMtall-autumn-57974
08/18/2025, 3:48 PMrefined-appointment-81829
08/18/2025, 3:49 PMtall-autumn-57974
08/18/2025, 3:50 PMroom.local_participant.publish_track
which triggers an error when executed before agent_session.start()
Exception: cannot access local participant before connecting {"pid": 5435, "job_id": "AJ_qXUY62tyzsXk"}
tall-autumn-57974
08/18/2025, 3:51 PMawait publish_audio_track(ctx.room)
after session start, but then the audio is played at the same time as the bot speaking...refined-appointment-81829
08/18/2025, 3:55 PMtall-autumn-57974
08/18/2025, 3:57 PMrefined-appointment-81829
08/18/2025, 3:57 PMtall-autumn-57974
08/18/2025, 3:58 PMtall-autumn-57974
08/18/2025, 3:59 PMrefined-appointment-81829
08/18/2025, 3:59 PMtall-autumn-57974
08/18/2025, 4:00 PMtall-autumn-57974
08/18/2025, 4:07 PMBackgroundAudioPlayer
is not able to play a remote file directly, right?refined-appointment-81829
08/18/2025, 4:08 PMtall-autumn-57974
08/18/2025, 4:09 PMawait publish_audio_track(ctx.room)
# Start background audio
await userdata.audio_player.start(room=ctx.room, agent_session=session)
await session.generate_reply()
tall-autumn-57974
08/18/2025, 4:09 PMtall-autumn-57974
08/18/2025, 4:09 PMtall-autumn-57974
08/18/2025, 4:10 PMrefined-appointment-81829
08/18/2025, 4:10 PMrefined-appointment-81829
08/18/2025, 4:11 PMtall-autumn-57974
08/18/2025, 4:13 PMrefined-appointment-81829
08/18/2025, 4:14 PM