Appearance
Build Real-Time Speech Recognition with Paraformer Java SDK
Reference, synced 2026-06-13.
flowchart TD n0["Models"] n1["Overview"] n2["Products"] n3["Solutions"] n4["Pricing"] n5["Resources"] n6["Partners"] n7["Support"] n8["Language"] n0 --> n1 n1 --> n2 n2 --> n3 n3 --> n4 n4 --> n5 n5 --> n6 n6 --> n7 n7 --> n8
Free access Accelerate Delivery with Fixed-Cost Agentic CodingWatch how it works
Models
Empowering AI innovation for both enterprises and developers with Alibaba Cloud’s best-in-class Qwen models, AI-native apps, and AI solutions.
Alibaba Cloud Model Studio \ Enterprise-grade large model service and application development platform.
Try Visual Model \ Supports image understanding, image generation, and video generation.
Models
HappyHorse-1.0-T2V \ Cinematic creative generation, ultimate dynamic details Qwen3-VL-Plus \ Native VL, spatial reasoning, 1M-context video analysis Wan2.7-VideoEdit \ Supports both localized and global editing with prompt
Qwen3.6-Plus \ Native multimodal, 1M context, agentic coding Wan2.7-Image-Pro \ Interactive editing, long-text rendering, precise prompt following Qwen-Plus \ Balanced intelligence, efficient inference, production-ready performance
Qwen-Image-2.0 \ Professional infographics, exquisite photorealism Z-Image-Turbo \ Ultra-fast image generation, high throughput, cost-optimized inference Qwen3-Coder-Next \ Multi-turn tool interactions, future-ready development support
Wan2.7-T2V \ High-fidelity T2V, 15s duration, advanced camera control Wan2.7-I2V \ Cinematic I2V with emotional depth and visceral impact Wan2.7-R2V \ Up to 5 mixed image/video inputs and audio timbre cloning
GenAI Application
Qoder \ Intelligent coding assistant, available for enterprise-dedicated deployment. Qoder CN \ AI-powered coding assistant that boosts developer productivity with intelligent code completion, AI chat, multi-file editing, and task automation.
AI Service
Model Experience \ Experience full-scale, multimodal model capabilities online. Platform for AI \ An AI-native algorithm engineering platform for end-to-end modeling, training, and inference service deployment. Fine-tune Video Generation Model \ Customize Wan’s text-to-video capabilities through model fine-tuning to meet your unique requirements.
AI Use Case
AI Savings Plan Hot \ Save up to 47% on AI costs. Limited-time offer tailored to your usage. AI Video Creation \ Elevate your professional video production with Wan 2.6.
AI Token Plan \ One plan. Multiple models. Big Savings with a Fixed Subscription. AI Image Creation \ All-in-one creative suite for copywriting, image generation, and poster design.
Overview
As a global full-stack AI leader, Alibaba Cloud aims to make computing accessible to everyone and help worldwide customers accelerate innovation.
Why Alibaba Cloud
About Alibaba Cloud \ AI Powered Cloud Technology Our Global Network \ Explore our global presence and deployment regions around the world Our Global Offices \ With offices in 4 continents, we're always close to where it matters.
Asia Accelerator \ Accelerate Success in Asia with Alibaba Cloud Go Global \ Benefits of our Global Alliance Trust Center \ Empowering enterprises with a secure, compliant, and globally trusted cloud infrastructure
Customers and Insights
Olympic Games \ Alibaba Cloud Powers Olympic Games with AI-powered cloud technology Case Studies \ Learn how customers are scaling their businesses on Alibaba Cloud Analyst Reports \ Learn what the top industry analyst firms are saying about Alibaba Cloud
What's New
Events and Webinars \ Quick access to upcoming and on-demand events Product Updates \ Stay informed of the latest innovations Press Room \ Latest news and media releases
Products
Featured ProductsAI & Machine Learning Computing Container Storage Networking & CDN Security Middleware Database Analytics ComputingMedia ServicesEnterprise Services & Cloud CommunicationDomain Names and WebsitesEnd User ComputingServerlessDeveloper ToolsMigration & O&M ManagementApsara Stack
Featured Products
Alibaba Cloud Model Studio \ Supercharge your AI journey effortlessly with industry-leading GenAI models ApsaraDB RDS \ Store and manage your business data, with automated monitoring and backups Certificate Management Service (Original SSL Certificate) \ Create a safe and secure connection between your website and users
Elastic Compute Service (ECS) \ Host websites anywhere and scale enterprise workloads Container Service for Kubernetes (ACK) \ Run and scale containerized applications on managed Kubernetes infrastructure Object Storage Service (OSS) \ Store large amounts of data in the cloud and access it anywhere, anytime
Simple Application Server (SAS) \ All-in-one services for fast deployment Elastic IP Address (EIP) \ Manage your public IPs independently to improve internet network quality Domain Names and Website \ Get the perfect domain name to suit your every need
Related Programs
Solutions
Solutions by Industry Technical Solutions AI WebsitesNetworking Security and ComplianceData and AnalyticsEnterprise Service and ApplicationCloud MigrationCloud NativeHybrid CloudSMB solutions
Financial Services \ Innovate faster with Alibaba Cloud Games \ Grow your game rapidly with high global availability
New Retail \ Alibaba Cloud enables digital retail transformation to fuel growth and realize an omnichannel customer experience throughout the consumer journey. Media and Entertainment \ Ready your content for today's media market with a digitalized media journey
Supply Chain \ Power your supply chain with intelligent, efficient, and reliable solutions Sports \ Digitizing the sports industry with intelligent tech
Sustainability \ Achieve a sustainable future with low-carbon and energy-efficient technologies
Pricing
Flexible options like pay-as-you-go and clear billing rules to meet diverse business needs.
Overview & Tools
Pricing Calculator \ Get an instant pricing estimate based on your usage and needs Free Trial \ Try our 80+ cloud products for free.
Pricing Options \ Get the most out of Alibaba Cloud with flexible pricing
Optimize your cost
Migrate & Save \ Superior Performance At Lower Pricing. Save up to 50%. Promotion Center \ Unlock the latest Alibaba Cloud offers & promos
Resources
Official documentation, extensive tools, training resources, and a community to grow and innovate in the cloud.
Technical Resources
Documentation \ Product guides and FAQs Architecture Center \ Design reliable, secure, and efficient cloud architecture. Intelligent Solution Explorer \ Find the right solution for you, powered by AI
Blog \ Latest cloud insights and developer trends Whitepapers \ Research that explores the how and why behind our technology
Training&Certification
Alibaba Cloud Academy \ Build cloud skills and earn certifications with expert-led training.
Developer Hub
Alibaba Cloud Project Hub \ Explore real-world projects built by developers using our platform. Our Developer MVPs \ Celebrating the developers who lead, build, and inspire our community
Partners
Partner-first strategy offering collaborative product, sales, and service models, plus high-quality partner solutions that complement Alibaba Cloud’s capabilities.
Marketplace
AI Alliance for ISVs \ Partner with us to build and grow AI solutions together ISV Benefits \ Unlock resources, market access, and go-to-market support as an ISV partner
Alibaba Cloud Marketplace \ Explore ready-to-deploy solutions from our partners and ISVs
Find a Partner
Partner Hub \ Find your ideal partner in no time
Become a Partner
Partner Network \ A partner portal for Alibaba Cloud Channel, Technology, MSP partner and other partner programs
Support
Full-lifecycle support and expert services, from cloud advisory and migration to operations.
Support & Professional Services
Professional Services \ Expert-led services to design, migrate, and optimize your cloud journey Support Plans \ Flexible support for every stage — from startup to enterprise
Partner Support Program \ Priority technical support for partners, with dedicated managers and faster issue resolution
Contact us
Connect With Us \
Talk to a sales expert and get a custom quote for your business
Language
- English
- 简体中文
- 繁體中文
- 日本語
- Bahasa Indonesia
Locale
Visit aliyun.com
Documentation
Alibaba Cloud Model Studio
User Guide (Models) User Guide (Application) API Reference (Models) API Reference (Application)
Search for Help Content
Getting Started
The Beginner's Guide
Well-Architected Framework
AI & Machine Learning
Platform For AI
Alibaba Cloud Model Studio
DashVector
Artificial Intelligence Recommendation
OpenSearch
Image Search
Machine Translation
Intelligent Speech Interaction
Optimization Solver
Intelligent Computing LINGJUN
Computing
Elastic Compute Service
Elastic GPU Service
Elastic Container Instance
Dedicated Host
Compute Nest
Simple Application Server
Cloud Box
Auto Scaling
Elastic High Performance Computing
Batch Compute (Deprecated)
Function Compute
Serverless App Engine
ENS
Elastic Desktop Service
App Streaming
WUYING Terminal
Cloud Phone
Edge Network Acceleration
Alibaba Cloud Linux
AgentBay
Container
Container Service for Kubernetes
Container Compute Service
Container Registry
Storage
Object Storage Service
Cloud Parallel File Storage
File Storage NAS
Tablestore
Storage Capacity Unit
Simple Log Service
Cloud Backup
Intelligent Media Management
Drive and Photo Service
Data Transport
Cloud Storage Gateway
Data Online Migration
Hybrid Cloud Storage Array
Storage Services Overview
Backup and Disaster Recovery Center
Networking and CDN
Server Load Balancer
Elastic IP Address
Internet Shared Bandwidth
Data Transfer Plan
Virtual Private Cloud
NAT Gateway
PrivateLink
Alibaba Cloud DNS PrivateZone
Network Intelligence Service
Cloud Data Transfer
IPv6 Gateway
Anycast Elastic IP Address
Cloud Enterprise Network
Global Accelerator
VPN Gateway
Smart Access Gateway
Express Connect
CDN
Edge Security Acceleration
Cloud Network Well-architected Design Guidelines
Security
Anti-DDoS
Web Application Firewall
Cloud Firewall
Security Center
Bastionhost
Secure Access Service Edge
Certificate Management Service
Key Management Service
Data Security Center
Identity as a Service
Fraud Detection
AI Guardrails
Captcha
Blockchain as a Service
ID Verification
Managed Security Service
Middleware
Enterprise Distributed Application Service
Microservices Engine
Alibaba Cloud Service Mesh
SchedulerX
ApsaraMQ for RocketMQ
ApsaraMQ for Kafka
ApsaraMQ for RabbitMQ
ApsaraMQ for MQTT
Simple Message Queue (formerly MNS)
CloudFlow
EventBridge
Application Real-Time Monitoring Service
Managed Service for Prometheus
Managed Service for Grafana
Managed Service for OpenTelemetry
Performance Testing
STAROps
Databases
ApsaraDB Console
PolarDB
ApsaraDB RDS
ApsaraDB for OceanBase (Deprecated)
Tair (Redis® OSS-Compatible)
Lindorm
Time Series Database
ApsaraDB for MongoDB
ApsaraDB for HBase
ApsaraDB for Memcache
ApsaraDB for MyBase
AnalyticDB
ApsaraDB for ClickHouse
ApsaraDB for SelectDB
Data Transmission Service
Database Autonomy Service
Data Management
Database Gateway - Deprecated
ApsaraDB for Cassandra - Deprecated
Analytics Computing
MaxCompute
Hologres
Realtime Compute for Apache Flink
Elasticsearch
Vector Retrieval Service for Milvus
E-MapReduce
Data Lake Formation
DataV
Quick BI
Quick Audience
Quick Tracking
DataWorks
DataHub
Dataphin
Media Services
ApsaraVideo VOD
ApsaraVideo Live
Intelligent Media Services
ApsaraVideo Media Processing
Apsara Video SDK
Enterprise Services & Cloud Communication
Energy Expert
CloudQuotation
Salesforce on Alibaba Cloud
GoChina ICP Filing Assistant
Marketplace
Alibaba Mail
Direct Mail
Short Message Service
Voice Service
Phone Number Verification Service
Cell Phone Number Service
Chat App Message Service
Financial Intelligence Engine
Domain Names and Websites
Domain Names
ICP Filing
Alibaba Cloud DNS
End User Computing
Elastic Desktop Service
App Streaming
WUYING Terminal
Cloud Phone
AgentBay
Internet of Things
IoT Platform
Serverless
Serverless App Engine
CloudFlow
EventBridge
Simple Message Queue (formerly MNS)
Function Compute
Developer Tools
OpenAPI Explorer
Alibaba Cloud SDK
Cloud Shell
Resource Orchestration Service
Alibaba Cloud CLI
BSS OpenAPI
Terraform
Pulumi
Ticket System API
Mobile Platform as a Service
Alibaba Cloud DevOps
API Gateway
Cloud Control API
AI Coding Assistant Lingma
Cloud Skills Portal
Migration & O&M Management
CloudOps Orchestration Service
Cloud Monitor
Intelligent Advisor
Cloud Governance Center
ActionTrail
Cloud Config
Resource Access Management
Resource Management
Cloud Architect Design Tools
Migration Hub
Server Migration Center
Service Catalog
Logic Composer
Quota Center
CloudSSO
HTTPDNS
Solutions
SAP
SuperApp
OpenLake
Membership Service
Expenses and Costs
Account Center
More
Support
Legal
Tech Share Terms and Conditions
After Sales Support
China Gateway Program
Service Level Objectives
Management Console
Security Control
The parameters and interfaces of the Paraformer real-time speech recognition Java SDK.
Important
This document applies only to the China (Beijing) region. To use the model, you must use an API key from the China (Beijing) region.
User guide: For model descriptions and selection guidance, see Real-time speech recognition - Fun-ASR/Paraformer.
Prerequisites
- You have activated the Model Studio and created an API key. Export it as an environment variable (not hard-coded) to prevent security risks.
Note
For temporary access or strict control over high-risk operations (accessing/deleting sensitive data), use a temporary authentication token instead.
Compared with long-term API keys, temporary tokens are more secure (60-second lifespan) and reduce API key leakage risk.
To use a temporary token, replace the API key used for authentication in your code with the temporary authentication token.
- Install the latest version of the DashScope SDK.
Model list
| paraformer-realtime-v2 | paraformer-realtime-8k-v2 |
|---|
| paraformer-realtime-v2 | paraformer-realtime-8k-v2 | |
| Scenarios | Scenarios such as live streaming and meetings | Recognition scenarios for 8 kHz audio, such as telephone customer service and voicemail |
| Sample rate | Any | 8 kHz |
| Languages | Chinese (including Mandarin and various dialects), English, Japanese, Korean, German, French, and Russian Supported Chinese dialects: Shanghainese, Wu, Minnan, Northeastern, Gansu, Guizhou, Henan, Hubei, Hunan, Jiangxi, Ningxia, Shanxi, Shaanxi, Shandong, Sichuan, Tianjin, Yunnan, and Cantonese | Chinese |
| Punctuation prediction | ✅ Supported by default. No configuration is required. | ✅ Supported by default. No configuration is required. |
| Inverse Text Normalization (ITN) | ✅ Supported by default. No configuration is required. | ✅ Supported by default. No configuration is required. |
| Custom hotwords | ✅ For more information, see Custom hotwords | ✅ For more information, see Custom hotwords |
| Specify recognition language | ✅ Specified by the language_hints parameter. | ❌ |
| Emotion recognition | ❌ | ✅ (Click for usage instructions) Constraints: - paraformer-realtime-8k-v2 only - Requires semantic_punctuation_enabled false (default) - Only available when isSentenceEnd() returns true To get the emotion recognition results, call the getEmoTag and getEmoConfidence methods of the Sentence information (Sentence) object. These methods return the emotion and confidence level for the current sentence. |
Getting started
The Recognition class provides interfaces for non-streaming and bidirectional streaming calls. Select a method based on your requirements:
- Non-streaming: Recognizes local files and returns complete results in a single response. For pre-recorded audio.
Non-streaming call
Submit a speech-to-text task for a local file and receive the complete result synchronously (blocking operation).
Instantiate Recognition and call call with request parameters and the file to recognize.
Click to view the full example
The audio file used in the example is: asr_example.wav.
java
import com.alibaba.dashscope.audio.asr.recognition.Recognition;
import com.alibaba.dashscope.audio.asr.recognition.RecognitionParam;
import java.io.File;
public class Main {
public static void main(String[] args) {
// Create a Recognition instance.
Recognition recognizer = new Recognition();
// Create a RecognitionParam.
RecognitionParam param =
RecognitionParam.builder()
// If you do not configure the API Key to an environment variable, uncomment the following line of code and replace yourApikey with your API Key.
// .apiKey("yourApikey")
.model("paraformer-realtime-v2")
.format("wav")
.sampleRate(16000)
// The "language_hints" parameter is supported only by the paraformer-realtime-v2 model.
.parameter("language_hints", new String[]{"zh", "en"})
.build();
try {
System.out.println("Recognition result: " + recognizer.call(param, new File("asr_example.wav")));
} catch (Exception e) {
e.printStackTrace();
} finally {
// Close the WebSocket connection after the task is complete.
recognizer.getDuplexApi().close(1000, "bye");
}
System.out.println(
"[Metric] requestId: "
+ recognizer.getLastRequestId()
+ ", first package delay ms: "
+ recognizer.getFirstPackageDelay()
+ ", last package delay ms: "
+ recognizer.getLastPackageDelay());
System.exit(0);
}
}Bidirectional streaming call: Based on callbacks
Submit speech-to-text tasks and receive streaming results via a callback interface.
- Start streaming speech recognition
Instantiate the Recognition class and call the call method with the request parameters and the callback interface (ResultCallback) to start streaming speech recognition.
- Stream audio
Call sendAudioFrame repeatedly to send binary audio segments (from local files or devices like microphones). While sending audio data, the server returns results in real time via onEvent callback.
Recommended: ~100 ms duration per segment, 1-16 KB size.
- End processing
Call the stop method of the Recognition class to stop speech recognition.
This method blocks the current thread until the onComplete or onError callback of the callback interface (ResultCallback) is triggered.
Click to view the full example
Recognize speech from a microphone
Recognize a local audio file
java
import com.alibaba.dashscope.audio.asr.recognition.Recognition;
import com.alibaba.dashscope.audio.asr.recognition.RecognitionParam;
import com.alibaba.dashscope.audio.asr.recognition.RecognitionResult;
import com.alibaba.dashscope.common.ResultCallback;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.TargetDataLine;
import java.nio.ByteBuffer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.submit(new RealtimeRecognitionTask());
executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.MINUTES);
System.exit(0);
}
}
class RealtimeRecognitionTask implements Runnable {
@Override
public void run() {
RecognitionParam param = RecognitionParam.builder()
// If you do not configure the API Key to an environment variable, replace apiKey with your API Key.
// .apiKey("yourApikey")
.model("paraformer-realtime-v2")
.format("pcm")
.sampleRate(16000)
// The "language_hints" parameter is supported only by the paraformer-realtime-v2 model.
.parameter("language_hints", new String[]{"zh", "en"})
.build();
Recognition recognizer = new Recognition();
ResultCallback<RecognitionResult> callback = new ResultCallback<RecognitionResult>() {
@Override
public void onEvent(RecognitionResult result) {
if (result.isSentenceEnd()) {
System.out.println("Final Result: " + result.getSentence().getText());
} else {
System.out.println("Intermediate Result: " + result.getSentence().getText());
}
}
@Override
public void onComplete() {
System.out.println("Recognition complete");
}
@Override
public void onError(Exception e) {
System.out.println("RecognitionCallback error: " + e.getMessage());
}
};
try {
recognizer.call(param, callback);
// Create an audio format.
AudioFormat audioFormat = new AudioFormat(16000, 16, 1, true, false);
// Match the default recording device based on the format.
TargetDataLine targetDataLine =
AudioSystem.getTargetDataLine(audioFormat);
targetDataLine.open(audioFormat);
// Start recording.
targetDataLine.start();
ByteBuffer buffer = ByteBuffer.allocate(1024);
long start = System.currentTimeMillis();
// Record for 50s and perform real-time transcription.
while (System.currentTimeMillis() - start < 50000) {
int read = targetDataLine.read(buffer.array(), 0, buffer.capacity());
if (read > 0) {
buffer.limit(read);
// Send the recorded audio data to the streaming recognition service.
recognizer.sendAudioFrame(buffer);
buffer = ByteBuffer.allocate(1024);
// The recording rate is limited. Sleep for a short period to prevent high CPU usage.
Thread.sleep(20);
}
}
recognizer.stop();
} catch (Exception e) {
e.printStackTrace();
} finally {
// Close the WebSocket connection after the task is complete.
recognizer.getDuplexApi().close(1000, "bye");
}
System.out.println(
"[Metric] requestId: "
+ recognizer.getLastRequestId()
+ ", first package delay ms: "
+ recognizer.getFirstPackageDelay()
+ ", last package delay ms: "
+ recognizer.getLastPackageDelay());
}
}The audio file used in the example is: asr_example.wav.
java
import com.alibaba.dashscope.audio.asr.recognition.Recognition;
import com.alibaba.dashscope.audio.asr.recognition.RecognitionParam;
import com.alibaba.dashscope.audio.asr.recognition.RecognitionResult;
import com.alibaba.dashscope.common.ResultCallback;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
class TimeUtils {
private static final DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
public static String getTimestamp() {
return LocalDateTime.now().format(formatter);
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.submit(new RealtimeRecognitionTask(Paths.get(System.getProperty("user.dir"), "asr_example.wav")));
executorService.shutdown();
// wait for all tasks to complete
executorService.awaitTermination(1, TimeUnit.MINUTES);
System.exit(0);
}
}
class RealtimeRecognitionTask implements Runnable {
private Path filepath;
public RealtimeRecognitionTask(Path filepath) {
this.filepath = filepath;
}
@Override
public void run() {
RecognitionParam param = RecognitionParam.builder()
// If you do not configure the API Key to an environment variable, replace apiKey with your API Key.
// .apiKey("yourApikey")
.model("paraformer-realtime-v2")
.format("wav")
.sampleRate(16000)
// The "language_hints" parameter is supported only by the paraformer-realtime-v2 model.
.parameter("language_hints", new String[]{"zh", "en"})
.build();
Recognition recognizer = new Recognition();
String threadName = Thread.currentThread().getName();
ResultCallback<RecognitionResult> callback = new ResultCallback<RecognitionResult>() {
@Override
public void onEvent(RecognitionResult message) {
if (message.isSentenceEnd()) {
System.out.println(TimeUtils.getTimestamp()+" "+
"[process " + threadName + "] Final Result:" + message.getSentence().getText());
} else {
System.out.println(TimeUtils.getTimestamp()+" "+
"[process " + threadName + "] Intermediate Result: " + message.getSentence().getText());
}
}
@Override
public void onComplete() {
System.out.println(TimeUtils.getTimestamp()+" "+"[" + threadName + "] Recognition complete");
}
@Override
public void onError(Exception e) {
System.out.println(TimeUtils.getTimestamp()+" "+
"[" + threadName + "] RecognitionCallback error: " + e.getMessage());
}
};
try {
recognizer.call(param, callback);
// Please replace the path with your audio file path.
System.out.println(TimeUtils.getTimestamp()+" "+"[" + threadName + "] Input file_path is: " + this.filepath);
// Read file and send audio by chunks.
FileInputStream fis = new FileInputStream(this.filepath.toFile());
// Set the chunk size to 1 second for a 16 kHz sample rate.
byte[] buffer = new byte[3200];
int bytesRead;
// Loop to read chunks of the file.
while ((bytesRead = fis.read(buffer)) != -1) {
ByteBuffer byteBuffer;
// Handle the last chunk which might be smaller than the buffer size.
System.out.println(TimeUtils.getTimestamp()+" "+"[" + threadName + "] bytesRead: " + bytesRead);
if (bytesRead < buffer.length) {
byteBuffer = ByteBuffer.wrap(buffer, 0, bytesRead);
} else {
byteBuffer = ByteBuffer.wrap(buffer);
}
recognizer.sendAudioFrame(byteBuffer);
buffer = new byte[3200];
Thread.sleep(100);
}
System.out.println(TimeUtils.getTimestamp()+" "+LocalDateTime.now());
recognizer.stop();
} catch (Exception e) {
e.printStackTrace();
} finally {
// Close the WebSocket connection after the task is complete.
recognizer.getDuplexApi().close(1000, "bye");
}
System.out.println(
"["\
+ threadName\
+ "][Metric] requestId: "
+ recognizer.getLastRequestId()
+ ", first package delay ms: "
+ recognizer.getFirstPackageDelay()
+ ", last package delay ms: "
+ recognizer.getLastPackageDelay());
}
}Bidirectional streaming call: Based on Flowable
You can submit a real-time speech-to-text task and receive streaming recognition results by implementing a Flowable workflow.
Flowable is a reactive stream type provided by RxJava, an open-source library for composing asynchronous and event-based programs that is released under the Apache 2.0 license. For more information, see Flowable API reference.
Click to view the full example
Directly call the streamCall method of the Recognition class to start recognition.
The streamCall method returns a Flowable<RecognitionResult> instance. Call methods of the Flowable instance, such as blockingForEach and subscribe, to process the recognition results. The results are encapsulated in RecognitionResult.
The streamCall method requires two parameters:
A
RecognitionParaminstance ( request parameters): Use this instance to set parameters for speech recognition, such as the model, sample rate, and audio format.A
Flowable<ByteBuffer>instance: Create an instance of theFlowable<ByteBuffer>type and implement a method within the instance to parse the audio stream.
java
import com.alibaba.dashscope.audio.asr.recognition.Recognition;
import com.alibaba.dashscope.audio.asr.recognition.RecognitionParam;
import com.alibaba.dashscope.exception.NoApiKeyException;
import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.TargetDataLine;
import java.nio.ByteBuffer;
public class Main {
public static void main(String[] args) throws NoApiKeyException {
// Create a Flowable<ByteBuffer>.
Flowable<ByteBuffer> audioSource =
Flowable.create(
emitter -> {
new Thread(
() -> {
try {
// Create an audio format.
AudioFormat audioFormat = new AudioFormat(16000, 16, 1, true, false);
// Match the default recording device based on the format.
TargetDataLine targetDataLine =
AudioSystem.getTargetDataLine(audioFormat);
targetDataLine.open(audioFormat);
// Start recording.
targetDataLine.start();
ByteBuffer buffer = ByteBuffer.allocate(1024);
long start = System.currentTimeMillis();
// Record for 50s and perform real-time transcription.
while (System.currentTimeMillis() - start < 50000) {
int read = targetDataLine.read(buffer.array(), 0, buffer.capacity());
if (read > 0) {
buffer.limit(read);
// Send the recorded audio data to the streaming recognition service.
emitter.onNext(buffer);
buffer = ByteBuffer.allocate(1024);
// The recording rate is limited. Sleep for a short period to prevent high CPU usage.
Thread.sleep(20);
}
}
// Notify that the transcription is complete.
emitter.onComplete();
} catch (Exception e) {
emitter.onError(e);
}
})
.start();
},
BackpressureStrategy.BUFFER);
// Create a Recognizer.
Recognition recognizer = new Recognition();
// Create a RecognitionParam and pass the created Flowable<ByteBuffer> to the audioFrames parameter.
RecognitionParam param = RecognitionParam.builder()
// If you do not configure the API Key to an environment variable, replace apiKey with your API Key.
// .apiKey("yourApikey")
.model("paraformer-realtime-v2")
.format("pcm")
.sampleRate(16000)
// The "language_hints" parameter is supported only by the paraformer-realtime-v2 model.
.parameter("language_hints", new String[]{"zh", "en"})
.build();
// Call the streaming interface.
recognizer
.streamCall(param, audioSource)
.blockingForEach(
result -> {
// Subscribe to the output result.
if (result.isSentenceEnd()) {
System.out.println("Final Result: " + result.getSentence().getText());
} else {
System.out.println("Intermediate Result: " + result.getSentence().getText());
}
});
// Close the WebSocket connection after the task is complete.
recognizer.getDuplexApi().close(1000, "bye");
System.out.println(
"[Metric] requestId: "
+ recognizer.getLastRequestId()
+ ", first package delay ms: "
+ recognizer.getFirstPackageDelay()
+ ", last package delay ms: "
+ recognizer.getLastPackageDelay());
System.exit(0);
}
}High-concurrency calls
The DashScope Java SDK uses OkHttp3 connection pooling to reduce the overhead of repeatedly establishing connections. For more information, see Optimize Paraformer real-time speech recognition for high concurrency.
Request parameters
Use the chained methods of RecognitionParam to configure parameters such as the model, sample rate, and audio format. Pass the configured parameter object to the call or streamCall method of the Recognition class.
Click to view an example
java
RecognitionParam param = RecognitionParam.builder()
.model("paraformer-realtime-v2")
.format("pcm")
.sampleRate(16000)
// The "language_hints" parameter is supported only by the paraformer-realtime-v2 model.
.parameter("language_hints", new String[]{"zh", "en"})
.build();| Parameter | Type | Default value | Required | Description |
|---|
| Parameter | Type | Default value | Required | Description |
| model | String | - | Yes | The model for real-time speech recognition. For more information, see Model list. |
| sampleRate | Integer | - | Yes | The audio sampling rate in Hz. This parameter varies by model: - paraformer-realtime-v2 supports any sample rate. - paraformer-realtime-8k-v2 supports only an 8000 Hz sample rate. |
| format | String | - | Yes | The format of the audio to be recognized. Supported audio formats: pcm, wav, mp3, opus, speex, aac, and amr. Important opus/speex: Must be encapsulated in Ogg. wav: Must be PCM encoded. amr: Only the AMR-NB type is supported. |
| vocabularyId | String | - | No | The ID of the hotword vocabulary. This parameter takes effect only when it is set. Use this field to set the hotword ID for v2 and later models. The hotword information for this hotword ID is applied to the speech recognition request. For more information, see Custom hotwords. |
| disfluencyRemovalEnabled | boolean | false | No | Specifies whether to filter out disfluent words: - true - false (default) |
| language_hints | String[] | ["zh", "en"] | No | The language code for recognition. If you cannot determine the language in advance, leave this parameter unset for automatic detection. Currently supported language codes: - zh: Chinese - en: English - ja: Japanese - yue: Cantonese - ko: Korean - de: German - fr: French - ru: Russian This parameter applies only to models that support multiple languages. For more information, see Model list. Note Set language_hints using the parameter or parameters method of the RecognitionParam instance: Set using the parameter method Set using the parameters method java RecognitionParam param = RecognitionParam.builder() .model("paraformer-realtime-v2") .format("pcm") .sampleRate(16000) .parameter("language_hints", new String[]{"zh", "en"}) .build(); java RecognitionParam param = RecognitionParam.builder() .model("paraformer-realtime-v2") .format("pcm") .sampleRate(16000) .parameters(Collections.singletonMap("language_hints", new String[]{"zh", "en"})) .build(); |
| semantic_punctuation_enabled | boolean | false | No | Specifies whether to enable semantic sentence segmentation (disabled by default): - true: Uses semantic segmentation (disables VAD segmentation). - false (default): Uses VAD segmentation. Semantic segmentation provides higher accuracy and is ideal for meeting transcription. VAD segmentation has lower latency and is ideal for interactive scenarios. Applies to v2 and later models. Note Set semantic_punctuation_enabled using the parameter or parameters method of the RecognitionParam instance: Set using the parameter method Set using the parameters method java RecognitionParam param = RecognitionParam.builder() .model("paraformer-realtime-v2") .format("pcm") .sampleRate(16000) .parameter("semantic_punctuation_enabled", true) .build(); java RecognitionParam param = RecognitionParam.builder() .model("paraformer-realtime-v2") .format("pcm") .sampleRate(16000) .parameters(Collections.singletonMap("semantic_punctuation_enabled", true)) .build(); |
| max_sentence_silence | Integer | 800 | No | The VAD sentence segmentation silence threshold (ms). If silence after a speech segment exceeds this value, the sentence ends. Range: 200-6000 ms. Default: 800 ms. Applies only when semantic_punctuation_enabled is false (VAD mode) and model is v2 or later. Note Set max_sentence_silence using the parameter or parameters method of the RecognitionParam instance: Set using the parameter method Set using the parameters method java RecognitionParam param = RecognitionParam.builder() .model("paraformer-realtime-v2") .format("pcm") .sampleRate(16000) .parameter("max_sentence_silence", 800) .build(); java RecognitionParam param = RecognitionParam.builder() .model("paraformer-realtime-v2") .format("pcm") .sampleRate(16000) .parameters(Collections.singletonMap("max_sentence_silence", 800)) .build(); |
| multi_threshold_mode_enabled | boolean | false | No | Specifies whether to prevent VAD from over-segmenting long sentences (disabled by default). Applies only when semantic_punctuation_enabled is false (VAD mode) and model is v2 or later. Note Set multi_threshold_mode_enabled using the parameter or parameters method of the RecognitionParam instance: Set using the parameter method Set using the parameters method java RecognitionParam param = RecognitionParam.builder() .model("paraformer-realtime-v2") .format("pcm") .sampleRate(16000) .parameter("multi_threshold_mode_enabled", true) .build(); java RecognitionParam param = RecognitionParam.builder() .model("paraformer-realtime-v2") .format("pcm") .sampleRate(16000) .parameters(Collections.singletonMap("multi_threshold_mode_enabled", true)) .build(); |
| punctuation_prediction_enabled | boolean | true | No | Specifies whether to automatically add punctuation to results (enabled by default): - true (default) - false Applies to v2 and later models only. Note Set punctuation_prediction_enabled using the parameter or parameters method of the RecognitionParam instance: Set using the parameter method Set using the parameters method java RecognitionParam param = RecognitionParam.builder() .model("paraformer-realtime-v2") .format("pcm") .sampleRate(16000) .parameter("punctuation_prediction_enabled", false) .build(); java RecognitionParam param = RecognitionParam.builder() .model("paraformer-realtime-v2") .format("pcm") .sampleRate(16000) .parameters(Collections.singletonMap("punctuation_prediction_enabled", false)) .build(); |
| heartbeat | boolean | false | No | Specifies whether to maintain a persistent server connection: - true: Keeps connection alive when sending silent audio continuously. - false (default): Connection times out after 60s even with silent audio. Silent audio: audio with no sound signal. Generate it with editing software (Audacity, Adobe Audition) or FFmpeg. Applies to v2 and later models only. Note To use this field, your SDK version must be 2.19.1 or later. Set heartbeat using the parameter or parameters method of the RecognitionParam instance: Set using the parameter method Set using the parameters method java RecognitionParam param = RecognitionParam.builder() .model("paraformer-realtime-v2") .format("pcm") .sampleRate(16000) .parameter("heartbeat", true) .build(); java RecognitionParam param = RecognitionParam.builder() .model("paraformer-realtime-v2") .format("pcm") .sampleRate(16000) .parameters(Collections.singletonMap("heartbeat", true)) .build(); |
| inverse_text_normalization_enabled | boolean | true | No | Specifies whether to enable Inverse Text Normalization (ITN). When enabled, Chinese numerals are converted to Arabic numerals (enabled by default). Applies to v2 and later models only. Note Set inverse_text_normalization_enabled using the parameter or parameters method of the RecognitionParam instance: Set using the parameter method Set using the parameters method java RecognitionParam param = RecognitionParam.builder() .model("paraformer-realtime-v2") .format("pcm") .sampleRate(16000) .parameter("inverse_text_normalization_enabled", false) .build(); java RecognitionParam param = RecognitionParam.builder() .model("paraformer-realtime-v2") .format("pcm") .sampleRate(16000) .parameters(Collections.singletonMap("inverse_text_normalization_enabled", false)) .build(); |
| apiKey | String | - | No | Your API key. |
Key interfaces
Recognition class
Import: com.alibaba.dashscope.audio.asr.recognition.Recognition. Key interfaces:
| Interface/Method | Parameters | Return value | Description |
|---|
| Interface/Method | Parameters | Return value | Description |
java public void call(RecognitionParam param, final ResultCallback<RecognitionResult> callback) | - param: Request parameters - callback: ResultCallback | None | Performs streaming recognition via callbacks (non-blocking). |
java public String call(RecognitionParam param, File file) | - param: Request parameters - file: The audio file to be recognized | Recognition result | Non-streaming call for local files. Blocks until file is fully read. Requires read permissions. |
java public Flowable<RecognitionResult> streamCall(RecognitionParam param, Flowable<ByteBuffer> audioFrame) | - param: Request parameters - audioFrame: A Flowable<ByteBuffer> instance | Flowable<RecognitionResult> | Performs streaming real-time recognition based on Flowable. |
java public void sendAudioFrame(ByteBuffer audioFrame) | - audioFrame: A binary audio stream of the ByteBuffer type | None | Pushes audio stream segments. Recommended: ~100 ms duration, 1-16 KB size per packet. Results are returned via onEvent callback. |
java public void stop() | None | None | Stops recognition. Blocks until onComplete or onError callback is triggered. |
java recognizer.getDuplexApi().close(int code, String reason) | code: WebSocket Close Code reason: Reason for closing For information about how to configure these two parameters, see The WebSocket Protocol document. | true | Close WebSocket after task ends to prevent leaks (even on exceptions). See Optimize Paraformer real-time speech recognition for high concurrency for connection reuse. |
java public String getLastRequestId() | None | requestId | Gets current task's request ID. Call after starting a task with call or streamingCall. Note This method is available in SDK versions 2.18.0 and later. |
java public long getFirstPackageDelay() | None | First-packet latency | Gets first-packet latency (delay from sending first audio packet to receiving first result). Call after task completion. Note This method is available in SDK versions 2.18.0 and later. |
java public long getLastPackageDelay() | None | Last-packet latency | Gets last-packet latency (time from sending stop to receiving last result). Call after task completion. Note This method is available in SDK versions 2.18.0 and later. |
ResultCallback
In bidirectional streaming, the server returns data via callbacks. Implement callback methods to handle server responses.
Inherit ResultCallback<RecognitionResult> to implement callbacks. RecognitionResult encapsulates server response data.
Because Java supports connection reuse, there are no onClose or onOpen methods.
Example
java
ResultCallback<RecognitionResult> callback = new ResultCallback<RecognitionResult>() {
@Override
public void onEvent(RecognitionResult result) {
System.out.println("RequestId is: " + result.getRequestId());
// Implement the logic to process the speech recognition result here.
}
@Override
public void onComplete() {
System.out.println("Task complete");
}
@Override
public void onError(Exception e) {
System.out.println("Task failed: " + e.getMessage());
}
};| Interface/Method | Parameters | Return value | Description |
|---|
| Interface/Method | Parameters | Return value | Description |
java public void onEvent(RecognitionResult result) | result: Real-time recognition result (RecognitionResult) | None | Called when server sends a response. |
java public void onComplete() | None | None | Called when task completes. |
java public void onError(Exception e) | e: Exception information | None | Called when an exception occurs. |
Response
Real-time recognition result (RecognitionResult )
RecognitionResult represents the result of a single real-time recognition.
| Interface/Method | Parameters | Return value | Description |
|---|
| Interface/Method | Parameters | Return value | Description |
java public String getRequestId() | None | requestId | Gets the request ID. |
java public boolean isSentenceEnd() | None | Whether the sentence is complete, which means a sentence break has occurred | Checks whether the given sentence has ended. |
java public Sentence getSentence() | None | Sentence information (Sentence) | Gets sentence info (timestamp and text). |
Sentence information (Sentence)
| Interface/Method | Parameters | Return value | Description |
|---|
| Interface/Method | Parameters | Return value | Description |
java public Long getBeginTime() | None | Sentence start time, in ms | Returns the start time of the sentence. |
java public Long getEndTime() | None | Sentence end time, in ms | Returns the end time of the sentence. |
java public String getText() | None | Recognized text | Returns the recognized text. |
java public List<Word> getWords() | None | A list of Word timestamp information (Word) objects | Returns word timestamp information. |
java public String getEmoTag() | None | Emotion of the current sentence | Returns sentence emotion: - positive: Happy, satisfied - negative: Angry, dull - neutral: No obvious emotion Constraints: - paraformer-realtime-8k-v2 only - Requires semantic_punctuation_enabled false (default) - Only available when isSentenceEnd() returns true |
java public Double getEmoConfidence() | None | Confidence level of the recognized emotion for the current sentence | Returns the confidence level of the recognized emotion for the current sentence. The value ranges from 0.0 to 1.0. A larger value indicates a higher confidence level. Constraints: - paraformer-realtime-8k-v2 only - Requires semantic_punctuation_enabled false (default) - Only available when isSentenceEnd() returns true |
Word timestamp information (Word )
| Interface/Method | Parameters | Return value | Description |
|---|
| Interface/Method | Parameters | Return value | Description |
java public long getBeginTime() | None | Word start time, in ms | Returns the start time of the word. |
java public long getEndTime() | None | Word end time, in ms | Returns the end time of the word. |
java public String getText() | None | Word | Returns the recognized word. |
java public String getPunctuation() | None | Punctuation | Returns the punctuation. |
Error codes
If an error occurs, see Error codes for troubleshooting.
If the problem persists, join the developer group to report the issue. Provide the Request ID to help us investigate the issue.
More examples
For more examples, see GitHub.
FAQ
Features
Q: How to maintain a persistent connection with the server during long periods of silence?
Set heartbeat parameter to true and send silent audio continuously.
Silent audio: audio with no sound signal. Generate it with editing software (Audacity, Adobe Audition) or FFmpeg.
Q: How to convert an audio format to the required format?
You can use the FFmpeg tool. For more information, see the official FFmpeg website.
bash
# Basic conversion command (universal template)
# -i: Specifies the input file path. Example: audio.wav
# -c:a: Specifies the audio encoder. Examples: aac, libmp3lame, pcm_s16le
# -b:a: Specifies the bit rate (controls audio quality). Examples: 192k, 320k
# -ar: Specifies the sample rate. Examples: 44100 (CD), 48000, 16000
# -ac: Specifies the number of sound channels. Examples: 1 (mono), 2 (stereo)
# -y: Overwrites an existing file (no value needed).
ffmpeg -i input_audio.ext -c:a encoder_name -b:a bit_rate -ar sample_rate -ac num_channels output.ext
# Example: WAV to MP3 (maintain original quality)
ffmpeg -i input.wav -c:a libmp3lame -q:a 0 output.mp3
# Example: MP3 to WAV (16-bit PCM standard format)
ffmpeg -i input.mp3 -c:a pcm_s16le -ar 44100 -ac 2 output.wav
# Example: M4A to AAC (extract/convert Apple audio)
ffmpeg -i input.m4a -c:a copy output.aac # Directly extract without re-encoding
ffmpeg -i input.m4a -c:a aac -b:a 256k output.aac # Re-encode to improve quality
# Example: FLAC lossless to Opus (high compression)
ffmpeg -i input.flac -c:a libopus -b:a 128k -vbr on output.opusQ: Can I view the time range for each sentence?
Yes. Results include start/end timestamps for each sentence to determine time ranges.
Q: How to recognize a local file (recorded audio file)?
There are two ways to recognize a local file:
Pass file path to call method for recognition.
Use the
sendAudioFramemethod of the Recognition class to send a binary stream to the server for recognition. For more information, see Bidirectional streaming call: Based on callbacks.Use the
streamCallmethod of the Recognition class to send a binary stream to the server for recognition. For more information, see Bidirectional streaming call: Based on Flowable.
Troubleshooting
Q: Why there is no recognition result?
Verify audio
formatandsampleRate/sample_ratematch parameter constraints. Common errors:The audio file has a .wav extension but is in MP3 format, and the
formatparameter is incorrectly set to `mp3`.The audio sample rate is 3600 Hz, but the
sampleRate/sample_rateparameter is incorrectly set to 48000.
Use ffprobe to check audio info (container, encoding, sample rate, channels):
bash
ffprobe -v error -show_entries format=format_name -show_entries stream=codec_name,sample_rate,channels -of default=noprint_wrappers=1 input.xxx- When you use the
paraformer-realtime-v2model, check whether the language set inlanguage_hintsmatches the actual language of the audio.
For example, the audio is in Chinese, but language_hints is set to en (English).
- If all the preceding checks pass, you can use custom hotwords to improve the recognition of specific words.
Previous: Python SDKNext: HTTP API
Is this page helpful?
Prerequisites
Model list
Getting started
Non-streaming call
Bidirectional streaming call: Based on callbacks
Bidirectional streaming call: Based on Flowable
High-concurrency calls
Request parameters
Key interfaces
Recognition class
ResultCallback
Response
Real-time recognition result (RecognitionResult)
Sentence information (Sentence)
Word timestamp information (Word)
Error codes
More examples
FAQ
Features
Troubleshooting
Contact Us
Sales Support
Live-chat with our sales team or get in touch with a business development professional in your region.
Contact Sales
Technical Support
Open a ticket and get quick help from our technical team.
Open a Ticket >
Connect & Report Abuse
We look forward to your suggestion.
Post a Suggestion > Report Abuse >
\ \ Hi, I'm Alibaba Cloud AI Assistant!\ \ I can help with questions and solutions.
Why Alibaba Cloud
About Alibaba Cloud
Asia Accelerator
Our Global Network
Global Offices
Trust Center
Case Studies
Analyst Reports
Products & Pricings
Pricing Calculator
ECS
SAS
Model Studio
Database
Security
SMS
Solutions
Financial Services
Retail Services
Media Services
Gaming Services
ISV Solutions
Engage
Developer Community
Partner Network
Startups
Marketplace
Join Alibaba Cloud
Resources & Support
Developer Learning Hub
Documentation Center
Training & Certification
Service Notices
Submit a Ticket
Security Report
Qwen Cloud
Careers About Us Privacy Policy Legal Integrity Compliance Reporting Channel Service Notices Links
© 2009-2026 Copyright by Alibaba Cloud All rights reserved
- YouTube
- TikTok
- contact.us@alibabacloud.com
- Call Us Now
- Discord
© 2009-2026 Copyright by Alibaba Cloud All rights reserved
Careers About Us Privacy Policy Legal Integrity Compliance Reporting Channel Service Notices Links