java 如何语音
Java 语音识别与合成实现
语音识别(Speech-to-Text) 使用Java实现语音识别可以通过开源库或云服务API。CMU Sphinx是一个流行的开源选择,支持离线识别。添加Maven依赖:
<dependency>
<groupId>edu.cmu.sphinx</groupId>
<artifactId>sphinx4-core</artifactId>
<version>5prealpha</version>
</dependency>
初始化识别器示例代码:
Configuration config = new Configuration();
config.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us");
config.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict");
config.setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin");
LiveSpeechRecognizer recognizer = new LiveSpeechRecognizer(config);
recognizer.startRecognition(true);
SpeechResult result;
while ((result = recognizer.getResult()) != null) {
System.out.println("识别结果: " + result.getHypothesis());
}
语音合成(Text-to-Speech) Java内置的javax.speech包支持基础TTS功能。需要安装Java Speech API实现:
import javax.speech.*;
import javax.speech.synthesis.*;
Synthesizer synth = Central.createSynthesizer(null);
synth.allocate();
synth.resume();
synth.speakPlainText("Hello world", null);
synth.deallocate();
云服务集成 Google Cloud Speech-to-Text API需要添加客户端库:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-speech</artifactId>
<version>2.6.1</version>
</dependency>
API调用示例:
try (SpeechClient speechClient = SpeechClient.create()) {
ByteString audioBytes = ByteString.readFrom(new FileInputStream("audio.wav"));
RecognitionConfig config = RecognitionConfig.newBuilder()
.setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
.setLanguageCode("en-US")
.build();
RecognitionAudio audio = RecognitionAudio.newBuilder().setContent(audioBytes).build();
RecognizeResponse response = speechClient.recognize(config, audio);
for (SpeechRecognitionResult result : response.getResultsList()) {
System.out.println("Transcript: " + result.getAlternatives(0).getTranscript());
}
}
Android平台实现 Android原生API提供更直接的语音支持。识别示例:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
startActivityForResult(intent, VOICE_REQUEST_CODE);
合成示例:
TextToSpeech tts = new TextToSpeech(context, status -> {
if (status == TextToSpeech.SUCCESS) {
tts.setLanguage(Locale.US);
tts.speak("Hello Android", TextToSpeech.QUEUE_FLUSH, null, null);
}
});
注意事项

- 离线方案如Sphinx需要预先下载语言模型
- 云服务API通常有免费额度限制
- Android实现需要麦克风和网络权限
- 实时处理需考虑音频缓冲和降噪处理






