java播放语音

使用JAVA实现语音朗读一段文本

需要做的工作:

1、下载  jacob-1.17-M2 或 jacob-1.18 (下载jar包,链接:https://pan.baidu.com/s/1qXPgCzy 密码:xigv)

2、解压jacob-1.17-M2 或 jacob-1.18 

3、向工程里导入jacob.jar

4、将 jacob-1.18-x64.dll 拷贝到jdk的bin目录和windows/system32目录

5、源代码如下

 

public static void main(String[] args) throws IOException {
    // 这个Sapi.SpVoice是需要安装什么东西吗,感觉平白无故就来了
    ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
    // Dispatch是做什么的?
    Dispatch sapo = sap.getObject();
    try {

        // 音量 0-100
        sap.setProperty("Volume", new Variant(100));
        // 语音朗读速度 -10 到 +10
        sap.setProperty("Rate", new Variant(-2));

        Variant defalutVoice = sap.getProperty("Voice");

        Dispatch dispdefaultVoice = defalutVoice.toDispatch();
        Variant allVoices = Dispatch.call(sapo, "GetVoices");
        Dispatch dispVoices = allVoices.toDispatch();

        Dispatch setvoice = Dispatch.call(dispVoices, "Item", new Variant(1)).toDispatch();
        ActiveXComponent voiceActivex = new ActiveXComponent(dispdefaultVoice);
        ActiveXComponent setvoiceActivex = new ActiveXComponent(setvoice);

        Variant item = Dispatch.call(setvoiceActivex, "GetDescription");
        // 执行朗读
        Dispatch.call(sapo, "Speak", new Variant("曾小明小朋友,早上好"));

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        sapo.safeRelease();
        sap.safeRelease();
    }
    }

 

如果不行:
1.戴上耳机试试;
2.检查下电脑上是否安装了office(金山的wps不行)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值