How can I route a room audio to my headset?
# ask-ai
p
How can I route a room audio to my headset?
t
Thanks for reaching out, Mario. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
p
I have a agent running locally and making an outbound call. I'd like to listen for the conversation in realtime.
t
Hi there! Routing room audio to a headset can vary a bit depending on the platform you're using, so let’s walk through it together: For Android: • First, start your audio session with
AudioSession.startAudioSession()
. • Then, you can route the audio to a wired headset by using
AudioSession.selectAudioOutput("headset")
. • Also, if you want the system to automatically choose audio outputs for you, you can set a preferred order using
preferredOutputList
inside
AudioConfiguration
. For iOS: • Good news! Audio usually routes automatically to connected headsets, whether wired or Bluetooth, when your audio session is set up properly. • If you’d like to give users manual control, you can call
AudioSession.showAudioRoutePicker()
to bring up the system’s audio route picker. Just a quick reminder: make sure you call
startAudioSession()
before trying to change audio outputs on either platform. If you have any questions or need further help, I’m here for you! Sources: docs/classes/AudioSession.html
p
Give me a sample python code please.
t
I'd love to share a helpful Python code snippet that lets you listen to an outbound call conversation in real-time using LiveKit. Let's walk through it together:
Copy code
import asyncio
import os
import json
from livekit import api
from livekit.agent import AgentSession, OutboundCaller
from livekit.job import JobContext
from livekit.stt import deepgram
from livekit.tts import cartesia
from livekit.llm import openai
from livekit.vad import silero
from livekit.turn_detection import EnglishModel

async def entrypoint(ctx: JobContext):
    await ctx.connect()

    # Parse metadata: phone number and transfer number
    dial_info = json.loads(ctx.job.metadata)
    phone_number = dial_info["phone_number"]
    participant_identity = phone_number

    # Initialize STT, TTS, LLM, and VAD
    session = AgentSession(
        stt=deepgram.STT(),
        tts=cartesia.TTS(),
        llm=openai.LLM(model="gpt-4o"),
        vad=silero.VAD.load(),
        turn_detection=EnglishModel(),
    )

    agent = OutboundCaller(
        name="AgentName",
        appointment_time="next Tuesday at 3pm",
        dial_info=dial_info,
    )

    # Start session before dialing to avoid missing audio
    session_started = asyncio.create_task(session.start(agent=agent, room=ctx.room))

    try:
        # Initiate outbound call
        await ctx.api.sip.create_sip_participant(
            api.CreateSIPParticipantRequest(
                room_name=ctx.room.name,
                sip_trunk_id=os.getenv("SIP_OUTBOUND_TRUNK_ID"),
                sip_call_to=phone_number,
                participant_identity=participant_identity,
                wait_until_answered=True,  # Blocks until call is answered
            )
        )

        # Wait for session and participant to join
        await session_started
        participant = await ctx.wait_for_participant(identity=participant_identity)
        agent.set_participant(participant)

        print(f"Call connected with {participant_identity}")
    except api.TwirpError as e:
        print(f"Error creating SIP participant: {e.message}")
        ctx.shutdown()
This snippet shows you how to: • Set up the agent with STT, TTS, LLM, and VAD functionalities. • Begin a session that processes audio in real-time. • Place an outbound call using
create_sip_participant
. • Listen in on the conversation as soon as the call connects. Just a quick reminder: please make sure you have your outbound SIP trunk configured and that the
SIP_OUTBOUND_TRUNK_ID
environment variable is set before you run this code. If you have any questions or need further help, I’m here for you!