Developing mobile apps with AI-generated sound effects is a challenge where speed and quality are paramount. Imagine: a user describes a sound "lightning strike with thunder and echo" — and within seconds receives a ready-to-use audio file. Behind this lies API integration, cache optimization, and low-latency playback configuration. We'll walk through connecting ElevenLabs Sound Effects or open-source AudioGen, implementing a 50 MB LRU cache, and achieving 10 ms latency on iOS.
How ElevenLabs Sound Effects API Works
Integrating the ElevenLabs Sound Effects API involves a direct POST request with a text description. The service returns a binary mp3 in 2–8 seconds. It is the simplest and highest-quality method for short sounds (0.5-5 sec).
POST https://api.elevenlabs.io/v1/sound-generation
xi-api-key: <key>
Content-Type: application/json
{
"text": "A heavy metal sword hitting a stone floor with a sharp clang and short reverb",
"duration_seconds": 2.0,
"prompt_influence": 0.3
}
prompt_influence ranges from 0 to 1: higher values yield more literal interpretation. For short effects (< 1 sec), we set 0.7–0.9.
The response is binary mp3 in the body (not JSON with URL). On mobile:
// iOS: direct download of binary response
func generateSoundEffect(description: String, duration: Double) async throws -> Data {
var request = URLRequest(url: URL(string: "https://api.elevenlabs.io/v1/sound-generation")!)
request.httpMethod = "POST"
request.setValue("audio/mpeg", forHTTPHeaderField: "Accept")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue(apiKey, forHTTPHeaderField: "xi-api-key")
request.httpBody = try JSONEncoder().encode(SoundGenRequest(
text: description, duration_seconds: duration, prompt_influence: 0.4
))
let (data, response) = try await URLSession.shared.data(for: request)
guard (response as? HTTPURLResponse)?.statusCode == 200 else {
throw SoundGenError.apiError
}
return data // mp3 bytes
}
Error Handling and Retries
On network failures or API rate limits (429 Too Many Requests), we implement exponential backoff with jitter. For ElevenLabs we recommend no more than 10 requests per minute, considering free-tier limits.
Why Caching Generated Sounds Matters
The same sound effect may be used multiple times — regenerating each time is expensive and slow. We cache by hash of prompt + duration:
// Android
class SoundEffectCache(private val cacheDir: File) {
private fun cacheKey(prompt: String, duration: Double): String =
"${prompt.hashCode()}_${(duration * 10).toInt()}.mp3"
fun getCached(prompt: String, duration: Double): File? {
val file = File(cacheDir, "sfx/${cacheKey(prompt, duration)}")
return if (file.exists()) file else null
}
fun saveToCache(prompt: String, duration: Double, data: ByteArray): File {
val dir = File(cacheDir, "sfx").also { it.mkdirs() }
val file = File(dir, cacheKey(prompt, duration))
file.writeBytes(data)
return file
}
}
We limit cache size to 50 MB with LRU eviction of older files. When exceeded, the least recently used files are removed.
Choosing Between ElevenLabs and AudioGen
ElevenLabs Sound Effects is a commercial service with excellent quality for short sounds (impact, steps, clicks). It suits quick-time events or UI sounds. AudioGen (via Replicate) is an open-source model, ideal for ambient sounds: rain, forest, wind. Comparison:
| Criteria | ElevenLabs Sound Effects | AudioGen (Replicate) |
|---|---|---|
| Short sound quality | Excellent (clean) | Average (possible artifacts) |
| Ambient quality | Good | Good (better for nature) |
| License | Proprietary | Open-source (MIT-like) |
| Generation time | 2–8 sec | 5–15 sec (polling) |
| API | Direct POST, binary response | Asynchronous, URL to mp3 |
Example AudioGen request:
POST https://api.replicate.com/v1/predictions
{
"version": "<audiogen-medium-hash>",
"input": {
"prompt": "Forest with birds and wind",
"duration": 5,
"top_k": 250
}
}
When to Choose AudioGen
If you need license purity — for example, a commercial game engine or an open-source project. AudioGen also handles longer ambient sounds (rain, wind) better.
Ensuring Low Playback Latency
For game applications, a sound effect must play instantly. AVAudioPlayer on iOS has 50–100 ms latency. For critical scenarios, we use AVAudioEngine with AVAudioPlayerNode:
let audioEngine = AVAudioEngine()
let playerNode = AVAudioPlayerNode()
audioEngine.attach(playerNode)
audioEngine.connect(playerNode, to: audioEngine.mainMixerNode, format: nil)
try audioEngine.start()
// Load file in advance, play instantly
let audioFile = try AVAudioFile(forReading: soundURL)
playerNode.scheduleFile(audioFile, at: nil)
playerNode.play() // Latency ~10 ms
On Android, for gaming we use Oboe (C++ NDK library from Google) or SoundPool for preloaded effects. Comparison:
| Method | Latency | Complexity | Memory |
|---|---|---|---|
| AVAudioPlayer | 50-100ms | Low | Low |
| AVAudioEngine + Node | ~10ms | Medium | Medium |
| Oboe (Android) | 5-10ms | High | Medium |
| SoundPool (Android) | 15-30ms | Low | High |
Background Playback and Interruptions
When the app is minimised, sound should stop or continue depending on the scenario. On iOS we handle AVAudioSessionInterruptionNotification, on Android — onPause() and onResume().
What You Need to Start AI Sound Generation on Mobile
Besides API keys and network access, it's critical to set up caching and provider selection. For ElevenLabs, you need an account and key; for AudioGen, a Replicate token. Caching by prompt saves traffic and speeds up reuse. We prepare documentation and a demo app for a quick start.
Our Process and Turnkey Timeline
- Analyze and select AI provider (ElevenLabs/AudioGen/custom model).
- Integrate API with response handling and caching.
- Implement low-latency playback.
- Test on various devices and OS versions.
- Optimize for app size and traffic.
Basic ElevenLabs integration with playback and cache — 2–3 days. With a library of user sounds, search, tagging, and video editor integration — 1–1.5 weeks. Pricing is customised per project.
What's Included
- Integration documentation and support during implementation.
- Source code with comments.
- Test scenarios and a demo app.
- Optimization and scalability recommendations.
Our Experience
We have developed over 15 mobile apps with AI features, including sound and music generation. Over 5 years in the market, expertise in Swift and Kotlin. For example, in a recent gaming project, we integrated ElevenLabs with AVPlayerNode, cutting playback latency from 80ms to under 10ms, and cached over 200 frequently used sounds. Get a consultation to find the best solution for your project. Contact us for a project evaluation.







