withPreconnectAudio

suspend fun <T> Room.withPreconnectAudio(timeout: Duration = TIMEOUT, topic: String = DEFAULT_TOPIC, onError: (Exception) -> Unit? = null, operation: suspend () -> T): T

Starts a pre-connect audio recording that will be sent to any agents that connect within the timeout. This speeds up preceived connection times, as the user can start speaking prior to actual connection with the agent.

This will automatically be cleaned up when the room disconnects or the operation fails.

Example:

try {
room.withPreconnectAudio {
// Audio is being captured automatically
// Perform any other (async) setup here
val (url, token) = tokenService.fetchConnectionDetails()
room.connect(
url = url,
token = token,
)
room.localParticipant.setMicrophoneEnabled(true)
}
} catch (e: Throwable) {
Log.e(TAG, "Error!")
}

Parameters

timeout

the timeout for the remote participant to subscribe to the audio track. The room connection needs to be established and the remote participant needs to subscribe to the audio track before the timeout is reached. Otherwise, the audio stream will be flushed without sending.

topic

the topic to send the preconnect audio buffer to. By default this is configured for use with LiveKit Agents.

onError

The error handler to call when an error occurs while sending the audio buffer.

operation

The connection lambda to call with the pre-connect audio.