Java Google 文字转语音:播放回复,不要转换为 mp3
Java Google Text to Speech: Play Reponse, Do not Convert to 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\"");
}
}
}
我修复了这个问题,我在我的依赖项中添加了一个 jplayer 然后我替换了然后我用这个替换了 mp3 部分:
BufferedInputStream inputStream = new BufferedInputStream(new ByteArrayInputStream(audioContents.toByteArray()));
Player player = new Player(inputStream); //player from jplayer
player.play();
我正在使用 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\"");
}
}
}
我修复了这个问题,我在我的依赖项中添加了一个 jplayer 然后我替换了然后我用这个替换了 mp3 部分:
BufferedInputStream inputStream = new BufferedInputStream(new ByteArrayInputStream(audioContents.toByteArray()));
Player player = new Player(inputStream); //player from jplayer
player.play();