How to play background audio during a function_too...
# ask-ai
b
How to play background audio during a function_tool call? Here is my code: @function_tool(description="Query") async def lookup(self, context: RunContext, query: str) -> str: audio_handle = None if self.background_audio_player: audio_handle = self.background_audio_player.play(AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING, volume=0.8))
t
Thanks for reaching out, Maxence. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're on the right track — your snippet shows the correct usage pattern. A few concise checks and options to make it work reliably: 1. Ensure the player is started before the tool runs:
Copy code
background_audio = BackgroundAudioPlayer()
await background_audio.start(room=ctx.room, agent_session=session)
self.background_audio_player = background_audio
1. Play and keep the handle so you can stop or await it:
Copy code
audio_handle = None
if self.background_audio_player:
    audio_handle = self.background_audio_player.play(
        AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING, volume=0.8)
    )

# When done or on interruption:
if audio_handle:
    audio_handle.stop()
    # or: await audio_handle  # to wait for completion
1. If the tool runs for a while and you want continuous sound, enable looping:
Copy code
self.background_audio_player.play(
    AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING, volume=0.8),
    loop=True
)
# stop when the operation completes
1. Handle interruptions (optional but recommended): • If user speech or other signals can interrupt the tool, stop the
PlayHandle
in your interruption path. If you'd like, share: • how you initialize the agent/session (so I can confirm
start()
usage), and • whether you want the sound to loop or stop when the lookup finishes. Sources: Tool definition and use | LiveKit docs | Agent speech and audio | LiveKit docs
b
It looks like a tool call with gemini realtime is turning off the audio for the thinking duration
t
Acknowledging your issue: you're seeing background audio stop during a Gemini Realtime tool call ("thinking" duration). I can help—quick clarifying questions first so I give the right fix. 1) Are you running the agent in Python or Node.js? 2) Do you use
thinking_sound
(agent-managed) or are you calling
background_audio_player.play(...)
manually inside the tool? 3) How and where do you initialize/start the
BackgroundAudioPlayer
(session-level vs inside the tool)? — paste the startup snippet if convenient. Quick guidance you can try right away • If you’re on Node.js: thinking sounds are not supported. Use manual on-demand playback with
background_audio_player.play(...)
and loop it during the tool call, then stop the handle when done. • If you’re on Python: thinking sounds are supported but Gemini Realtime can still mute auxiliary audio during realtime processing. Use manual playback (start looped clip with
play(..., loop=True)
) at session scope and stop the returned
PlayHandle
in a finally block of the tool so it keeps playing through the thinking period. If you share the three items above, I’ll give the exact code changes to keep audio playing during the Gemini Realtime tool call. Sources: Agent speech and audio | LiveKit docs
b
I am using python, calling background_audio_player.play() manually inside the tool, with BackgroundAudioPlayer initialized at session-level @breezy-insurance-66772.rtc_session() async def my_agent(ctx: JobContext): background_audio = BackgroundAudioPlayer( ambient_sound=AudioConfig(BuiltinAudioClip.OFFICE_AMBIENCE, volume=0.8) ) tools = Tools(config.agent_id, background_audio) await session.start( agent=VisionAssistant(config, tools), room=ctx.room, room_input_options=RoomInputOptions(video_enabled=True), ) await background_audio.start(room=ctx.room, agent_session=session) class Tools: @function_tool(description="Query") async def lookup(self, context: RunContext, query: str) -> str: audio_handle = None if self.background_audio_player: audio_handle = self.background_audio_player.play(AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING, volume=0.8))
t
Thanks — this is enough to give a concrete fix. Short answer: Gemini Realtime can take exclusive control of audio output and mute other tracks, so your current manual
play()
inside the tool may be getting cut off. The reliable fix in Python is to use BackgroundAudioPlayer’s
thinking_sound
(session-level) so the player automatically keeps the thinking/typing audio during tool execution. If that isn't viable, start the playback before the tool work and keep the returned
PlayHandle
alive for the tool duration (or use Gemini text-only + your own TTS). Two options with concise code you can apply now: 1) Recommended — enable thinking_sound at session init (auto-plays during function_tool)
Copy code
background_audio = BackgroundAudioPlayer(
    ambient_sound=AudioConfig(BuiltinAudioClip.OFFICE_AMBIENCE, volume=0.8),
    thinking_sound=AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING, volume=0.8),
)
await background_audio.start(room=ctx.room, agent_session=session)
# No need to call play() inside lookup; thinking_sound will play automatically.
2) Fallback — manually play before work and keep the handle alive
Copy code
@function_tool(...)
async def lookup(self, context: RunContext, query: str) -> str:
    audio_handle = None
    try:
        if self.background_audio_player:
            audio_handle = self.background_audio_player.play(
                AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING, volume=0.8),
                loop=True
            )
        result = await self._perform_search(query)  # your work
        return result
    finally:
        if audio_handle:
            audio_handle.stop()  # or `await audio_handle` to let it finish
If you prefer full control over mixing (no Gemini audio muting), run Gemini in text-only mode and use your own TTS for speech output. Do you want the exact patch to your current file to switch to
thinking_sound
, or should I produce the fallback changes integrated into your Tools class? Sources: Agent speech and audio | LiveKit docs | Gemini Live API plugin | LiveKit docs | Realtime model plugins | LiveKit Docs