Java实现语音播放功能

1、首先添加依赖

 <dependency>
            <groupId>com.hynnet</groupId>
            <artifactId>jacob</artifactId>
            <version>1.18</version>
</dependency>

配置jacob包,需要添加到java环境变量路径下(如果不知道在哪里的话,可以先不配置,直接运行以下代码,ide会报错,提示你应该放到哪个路径下);

链接:https://pan.baidu.com/s/1RIF1L-I2KefWGeDkUO3A9w
提取码:13h5

2、代码实现

可以自定义改变音速和声音大小

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComFailException;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class TextToSpeech {

    public static void textToSpeech(String text) throws Exception {
        ActiveXComponent ax = null;
        try {
            ax = new ActiveXComponent("Sapi.SpVoice");

            // 运行时输出语音内容
            Dispatch spVoice = ax.getObject();
            // 音量 0-100
            ax.setProperty("Volume", new Variant(100));
            // 语音朗读速度 -10 到 +10(-10语速最慢,10语速最快)
            ax.setProperty("Rate", new Variant(2));
            /*  执行朗读,默认调用系统中的TTS语音播放引擎,若本机没有语音设备
            可能会抛出异常(com.jacob.com.ComFailException: Invoke of: Speak) */
            Dispatch.call(spVoice, "Speak", new Variant(text));

            // 下面是构建文件流生成语音文件
            ax = new ActiveXComponent("Sapi.SpFileStream");
            Dispatch spFileStream = ax.getObject();

            ax = new ActiveXComponent("Sapi.SpAudioFormat");
            Dispatch spAudioFormat = ax.getObject();

            // 设置音频流格式
            Dispatch.put(spAudioFormat, "Type", new Variant(22));
            // 设置文件输出流格式
            Dispatch.putRef(spFileStream, "Format", spAudioFormat);
            // 调用输出 文件流打开方法,创建一个.wav文件
            Dispatch.call(spFileStream, "Open", new Variant("./text.wav"),
                    new Variant(3), new Variant(true));
            // 设置声音对象的音频输出流为输出文件对象
            Dispatch.putRef(spVoice, "AudioOutputStream", spFileStream);
            // 设置音量 0到100
            Dispatch.put(spVoice, "Volume", new Variant(100));
            // 设置朗读速度
            Dispatch.put(spVoice, "Rate", new Variant(-2));
            // 开始朗读
            Dispatch.call(spVoice, "Speak", new Variant(text));

            // 关闭输出文件
            Dispatch.call(spFileStream, "Close");
            Dispatch.putRef(spVoice, "AudioOutputStream", null);

            spAudioFormat.safeRelease();
            spFileStream.safeRelease();
            spVoice.safeRelease();
            ax.safeRelease();

        } catch (ComFailException e) {
            //e.printStackTrace();
            throw new Exception("没有可用的音频,请连接外接设备(耳机或音箱播放)");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3、添加测试类

直接调用上述方法即可;

import com.xin.voice.TextToSpeech;

public class TextToSpeechTest {

    public static void main(String[] args) {
        try {
            // 编写main方法直接调用上面的方法进行运行测试即可
            System.out.println("开始播放");
            //语速可以自行调节
            TextToSpeech.textToSpeech("主人,结算提交成功,请及时支付订单");
            System.out.println("播放结束");
        }  catch (Exception e) {
            e.printStackTrace();
            System.out.println("播放失败");
        }
        System.out.println("程序运行结束");
    }
}
posted @ 2022-06-19 20:15  进阶的蜗牛  阅读(1737)  评论(1)    收藏  举报