Azure的tts文本转语音服务使用

该代码示例演示了如何使用Java和MicrosoftAzureCognitiveServices的语音服务将文本转换为音频文件(wav格式)。程序配置了特定的语音样式和停顿,输入文本后生成对应的音频并保存到指定路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用文件接收音频文件即可跳过播报过程:

package com.tts;

import com.microsoft.cognitiveservices.speech.*;
import com.microsoft.cognitiveservices.speech.audio.AudioConfig;

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * 文本转语音
 * @author dftdla
 */
public class SpeechSynthesis {

    private static final String speechKey;
    private static final String speechRegion;
    //情绪度
    private static final String styleDegree = "1.4";
    //情绪
    private static final String style = "excited";
    //停顿点持续时间
    private static final String commasilenceExact = "600ms";
    //设置超时时间为5s
    private static final long timeout = 5;
    private static final TimeUnit unit = TimeUnit.SECONDS;

    public static void main(String[] args) throws InterruptedException, ExecutionException, IOException {

        //替换成自己的key和regin
        SpeechConfig speechConfig = SpeechConfig.fromSubscription(speechKey, speechRegion);

        //azure中菲律宾的女音 : fil-PH-BlessicaNeural
        speechConfig.setSpeechSynthesisVoiceName("en-IE-EmilyNeural");

        SpeechSynthesizer speechSynthesizer;
        String text;
        Scanner scanner = new Scanner(System.in);
        text = scanner.next();

        speechConfig.setSpeechSynthesisOutputFormat(SpeechSynthesisOutputFormat.Riff24Khz16BitMonoPcm);

        File file = new File("E:\\home\\admin\\source\\upload\\audio\\00a4da29-eec2-4464-8315-d9d7789f7240.wav");
        file.getParentFile().mkdirs();

        AudioConfig audioConfig = AudioConfig.fromWavFileOutput(file.getAbsolutePath());

        try {
            speechSynthesizer = new SpeechSynthesizer(speechConfig, audioConfig);

            SpeechSynthesisResult result = speechSynthesizer.SpeakText(text);

            speechSynthesizer.StartSpeakingText(Arrays.toString(result.getAudioData()));
//            speechSynthesizer.waitUntilDone();
            speechSynthesizer.StopSpeakingAsync();
        } catch(Exception ex) {
            System.out.println(ex.getMessage()+"??");
        } finally {
            System.out.println("done");
        }
        System.exit(0);
    }
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱飞的男孩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值