New AssemblyAI-TTS-1: Text-to-speech for voice agents. Learn more
Insights & Use Cases

How to use AssemblyAI with Java

Use AssemblyAI's Speech AI models from Java with the built-in HttpClient — transcribe files, stream audio in real time, apply LLMs with LLM Gateway, and run sentiment analysis.

Abstract green cylinder illustration

Written by

Martin Schweiger

Published on

21 August 2026

Learn how to use AssemblyAI's Speech AI models directly with Java using the built-in HttpClient. Transcribe audio, analyze audio using audio intelligence models, and apply LLMs to your audio data using LLM Gateway.

1. Transcribe an audio file

You can also transcribe a local file, as shown here.

2. Transcribe audio in real-time

3. Use LLM Gateway to build LLM apps on voice data

Learn how to use LLMs with audio data using LLM Gateway in our docs.

4. Use Speech Understanding models

Learn more about our Speech Understanding models in our docs.

Start Building Voice AI in Java

Transcription, real-time streaming, LLM Gateway, and Speech Understanding all run off the same API key. Get one free and ship your first integration today.

Sign up free

Frequently asked questions

Is the Java Speech API still supported?

No — the Java Speech API (JSAPI) is a legacy specification and is not how speech recognition is done in Java today. It never shipped a maintained reference implementation, and the follow-on JSR has been inactive for years. Modern Java applications call a cloud speech-to-text API over HTTP or WebSocket instead, which is what the code in this guide does with the JDK's built-in HttpClient.

Do I need an SDK to use a speech-to-text API in Java?

No. AssemblyAI's API is plain HTTP and WebSocket, so Java's built-in java.net.http.HttpClient is enough — you POST to https://api.assemblyai.com/v2/transcript with your API key in the Authorization header and read the JSON response. The only external dependency in this guide is a JSON parser (Gson). Skipping the SDK means one less version to track and no wrapper between you and the API surface.

Are transcripts returned immediately, or do I need to poll?

Transcription is asynchronous: the submit request returns a transcript ID right away, and the transcript itself is ready a short time later. The simplest approach is polling the GET /v2/transcript/{id} endpoint until status is completed, which is what the examples here do. In production, use a webhook instead — pass a webhook_url when you submit and AssemblyAI POSTs to your endpoint on completion, so you're not holding threads open in a sleep loop.

How do I transcribe audio in real time in Java?

Open a WebSocket to wss://streaming.assemblyai.com/v3/ws using HttpClient.newWebSocketBuilder(), authenticate with your API key in the header, and send raw PCM16 mono audio at 16kHz as binary frames in roughly 50ms chunks. Your listener receives Begin, Turn, and Termination messages as JSON — each Turn carries the running transcript plus an end_of_turn flag telling you when the speaker finished. Always send a Terminate message before closing, since streaming sessions bill by connection duration.

Is there a free API for speech-to-text?

Most production-grade speech-to-text APIs, including AssemblyAI, offer free credits rather than a permanently free tier — enough to build and test an integration before you pay anything. Fully free options exist as self-hosted open-source models, but you take on the GPU cost, the deployment, and the accuracy gap, which is usually a worse trade than a per-hour API for anything user-facing.

What audio file formats work with a speech-to-text API?

AssemblyAI accepts the common formats directly — MP3, WAV, FLAC, M4A, OGG, and WebM — with a 5GB maximum file size, so there's no transcoding step for most inputs. WAV and FLAC preserve the most audio detail and give the highest accuracy ceiling, while MP3 at 128kbps or above is a reasonable trade when bandwidth matters. For real-time streaming the requirement is stricter: raw PCM16 mono at 16kHz, matching the sample_rate you set on the connection.