How to start and stop a background music inside a ...
# ask-ai
h
How to start and stop a background music inside a function tool?
t
Thanks for reaching out, James. 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 get started with playing background music inside a function tool in LiveKit using the
BackgroundAudioPlayer
class from the Python SDK. Here's a friendly step-by-step guide to make it easy for you: 1. Initialize the BackgroundAudioPlayer: Begin by creating an instance, and you can even add some ambient or thinking sounds to set the mood.
Copy code
from livekit.agents import BackgroundAudioPlayer, AudioConfig, BuiltinAudioClip

background_audio = BackgroundAudioPlayer(
    ambient_sound=AudioConfig(BuiltinAudioClip.OFFICE_AMBIENCE, volume=0.8),
    thinking_sound=[
        AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING, volume=0.8),
        AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING2, volume=0.7),
    ],
)
2. Start the Player: Once your room and session are ready, you can start the playback during agent initialization.
await background_audio.start(room=ctx.room, agent_session=session)
3. Play Custom Audio Inside a Tool: Whenever you want to play a specific sound on demand, just use
.play()
inside your function tool.
Copy code
@function_tool
async def search_web(self, query: str) -> str:
    background_audio.play("filepath.ogg")
    await asyncio.sleep(5)
    return "Search completed successfully."
4. Stop the Player: And when you need to stop all background audio smoothly, simply call:
await background_audio.stop()
Just a quick note: supported audio formats include
.ogg
,
.mp3
, and
.wav
. Make sure the player starts after your room and session are fully ready. If you have any questions or want me to walk you through any part, feel free to ask—I'm here to help! Sources: livekit-agents/livekit/agents/voice/agent_activity.py | complex-agents/drive-thru/session_setup.py | Agent speech and audio | LiveKit Docs | External data and RAG | LiveKit Docs
p
can I dispatch locally runing agent to livekit room?
t
Absolutely! You can definitely dispatch a locally running agent to a LiveKit room. To make this happen, the agent needs to be registered with an
agent_name
in its
WorkerOptions
. Here are the two main ways to do it: 1. Via API: You can use the
AgentDispatchService
API to explicitly dispatch the agent to a room. Don’t worry if the room doesn’t exist yet—it will be created automatically. There are helpful methods like
createDispatch()
available in server SDKs such as Node.js and Python. 2. Via Access Token: Another way is to set up the agent dispatch in the
roomConfig
of a participant’s access token. Once the participant joins the room, the specified agent will be dispatched automatically. If you’d like, I’d be happy to provide detailed examples or walk you through the implementation step-by-step. Just let me know how I can help! Sources: Agent dispatch | LiveKit Docs | packages/livekit-server-sdk/src/AgentDispatchClient.ts | examples/agent_dispatch.py | README.md