ElevenLabs Streaming TTS Player (voice/ElevenLabsStreamingTts.kt)

safe-openclaw · apps/android/app/src/main/java/ai/openclaw/app/voice/ElevenLabsStreamingTts.kt · kotlin · MIT
realtime, voice, lang:kotlin, tag:android

Streams text to ElevenLabs WebSocket API and plays audio in real-time on Android. Reuse for low-latency voice responses in mobile apps.

// Source: https://github.com/Yapie0/safe-openclaw | License: MIT | Copyright 2025 Peter Steinberger
// Provided via justclaw.it.com; keep this notice with the code.
package ai.openclaw.app.voice

import android.media.AudioAttributes
import android.media.AudioFormat
import android.media.AudioManager
import android.media.AudioTrack
import android.util.Base64
import android.util.Log
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import okhttp3.*
import org.json.JSONObject
import kotlin.math.max

/**
 * Streams text chunks to ElevenLabs WebSocket API and plays audio in real-time.
 *
 * Usage:
 *   1. Create instance with voice/API config
 *   2. Call [start] to open WebSocket + AudioTrack
 *   3. Call [sendText] with increm

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key