Java Google Text to Speech:播放响应,不转换为 mp3

发布于 2025-01-20 07:37:06 字数 1443 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-01-27 07:37:06

我修复了这个,我在依赖项中添加了一个Jplayer,然后我替换了,然后用以下方式替换了mp3部分:

BufferedInputStream inputStream = new BufferedInputStream(new ByteArrayInputStream(audioContents.toByteArray()));
        Player player = new Player(inputStream); //player from jplayer
        player.play();

i fixed this, i added a jplayer to my dependencies then i replaced then i replaced the mp3 part with this:

BufferedInputStream inputStream = new BufferedInputStream(new ByteArrayInputStream(audioContents.toByteArray()));
        Player player = new Player(inputStream); //player from jplayer
        player.play();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文