Java Text to Speech
Call the Narakeet text to speech API from Java to produce audio files programmatically. The example on this page uses Apache HttpClient, but any HTTP library works — the API is a straightforward REST endpoint. No proprietary SDK or JNI binding required.
Refer to the Text to Speech API documentation for endpoint details, authentication setup, and features shared across all programming languages.
Text to Speech in Java
The method below sends a string to the API and writes the MP3 response to disk. It demonstrates text to speech in Java with minimal boilerplate.
public static void main(String[] args) throws java.io.FileNotFoundException, java.io.IOException {
String apiKey = Objects.requireNonNull(System.getenv("NARAKEET_API_KEY"), "NARAKEET_API_KEY environment variable is not set");
String voice = "raymond";
String text = "Hello from Java!";
String url = String.format("https://api.narakeet.com/text-to-speech/mp3?voice=%s", voice);
String outputFilePath = "output.mp3";
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Accept", "application/octet-stream");
httpPost.setHeader("Content-Type", "text/plain");
httpPost.setHeader("x-api-key", apiKey);
byte[] utf8Bytes = text.getBytes(StandardCharsets.UTF_8);
ByteArrayEntity requestBody = new ByteArrayEntity(utf8Bytes);
httpPost.setEntity(requestBody);
FileOutputStream outputStream = new FileOutputStream(outputFilePath);
HttpResponse response = httpClient.execute(httpPost);
response.getEntity().writeTo(outputStream);
outputStream.close();
}
The NARAKEET_API_KEY environment variable must be set before launching. Get a key from the API Keys dashboard. The full Maven project is at text-to-speech-api-java-example on GitHub.
Java Text to Voice
The API is a plain REST endpoint, so any Java HTTP client works for Java text to voice — Apache HttpClient (shown above), OkHttp, or even java.net.HttpURLConnection from the standard library. Pick whichever your project already uses; there is no dedicated Java text to voice SDK to evaluate or maintain.
Situations where Java text to voice is particularly effective:
- Spring Boot microservices that generate on-demand audio responses
- Batch jobs in enterprise systems that narrate daily financial or operational reports
- Android applications that need consistent server-side voice quality across devices
- Jenkins or Gradle plugins that produce spoken test summaries
- Kafka consumers that turn incoming messages into audio notifications
When input exceeds 1 KB or you want uncompressed WAV, use the Long Content (Polling) API. See the Java polling example for a complete implementation.
Java Text to Speach
If you have been searching for a Java text to speach solution, the Narakeet API is the most direct path. Traditional Java text to speach options like FreeTTS or MaryTTS require bundling large voice models and produce dated-sounding output. The API sidesteps all of that — your application sends text over HTTPS and receives studio-quality audio from 900 AI voices in 100 languages. No voice files to ship, no native dependencies, and identical results on every JVM.
Java Text to Speech API Options
Modify the query string on the endpoint URL to adjust the audio:
- voice — Choose a speaker by name, e.g.
?voice=raymond. The voice catalogue lists all 900 options. - voice-speed — Decimal multiplier.
1.3reads 30% faster;0.7reads 30% slower. - voice-volume —
soft,medium, orloud. - format — Swap
/mp3in the URL for/m4a(smaller files, similar quality) or/wav(uncompressed, polling API only).
Advanced features like pitch control and multi-voice scripts use the script header inside the request body. Details at Configuring Audio Tasks.