Java Google Text to Speech:播放响应,不转换为 mp3
我正在使用 google 文本转语音 API,我试图弄清楚如何立即播放 google 响应,而不是将其转换为 mp3 文件
public static void TTS(String word) throws IOException {
authExplicit();
try (
// Set the text input to be synthesized
SynthesisInput input = SynthesisInput.newBuilder().setText(word).build();
// Build the voice request, select the language code ("en-US") and the ssml voice gender
// ("neutral")
VoiceSelectionParams voice =
VoiceSelectionParams.newBuilder()
.setLanguageCode("en-US")
.setSsmlGender(SsmlVoiceGender.NEUTRAL)
.build();
// Select the type of audio file you want returned
AudioConfig audioConfig =
AudioConfig.newBuilder().setAudioEncoding(AudioEncoding.MP3).build();
// Perform the text-to-speech request on the text input with the selected voice parameters and
// audio file type
SynthesizeSpeechResponse response =
textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);
// Get the audio contents from the response
ByteString audioContents = response.getAudioContent();
// HERE, I DO NOT WANT TO CONVERT TO MP3. I just want the audio played out.....
try (OutputStream out = new FileOutputStream("output.mp3")) {
out.write(audioContents.toByteArray());
System.out.println("Audio content written to file \"output.mp3\"");
}
}
}
I am using the google text-to-speech API and I am trying to figure out how I'd be able to play the google response immediately rather than converting it to a mp3 file
public static void TTS(String word) throws IOException {
authExplicit();
try (
// Set the text input to be synthesized
SynthesisInput input = SynthesisInput.newBuilder().setText(word).build();
// Build the voice request, select the language code ("en-US") and the ssml voice gender
// ("neutral")
VoiceSelectionParams voice =
VoiceSelectionParams.newBuilder()
.setLanguageCode("en-US")
.setSsmlGender(SsmlVoiceGender.NEUTRAL)
.build();
// Select the type of audio file you want returned
AudioConfig audioConfig =
AudioConfig.newBuilder().setAudioEncoding(AudioEncoding.MP3).build();
// Perform the text-to-speech request on the text input with the selected voice parameters and
// audio file type
SynthesizeSpeechResponse response =
textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);
// Get the audio contents from the response
ByteString audioContents = response.getAudioContent();
// HERE, I DO NOT WANT TO CONVERT TO MP3. I just want the audio played out.....
try (OutputStream out = new FileOutputStream("output.mp3")) {
out.write(audioContents.toByteArray());
System.out.println("Audio content written to file \"output.mp3\"");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我修复了这个,我在依赖项中添加了一个Jplayer,然后我替换了,然后用以下方式替换了mp3部分:
i fixed this, i added a jplayer to my dependencies then i replaced then i replaced the mp3 part with this: