Java实现实现语音播报功能

在开发中遇到一个需求,只要来了一个新订单,后台和对应的物流公司就进行语音播报。

注意:此方法只适合在window中使用,在Linux中也就是服务器上是运行不了的,我使用的是另外一种方法,有时间会写出来,但觉得该方法也不错,特此记录一下

1、pom.xml引入jar包依赖

<!--  文字转语音 -->
<dependency>
  <groupId>com.hynnet</groupId>
  <artifactId>jacob</artifactId>
  <version>1.18</version>
</dependency>

2、把jacob-1.18-x64.dll文件复制到jdk安装位置的bin目录下。

文件下载路劲:https://files.cnblogs.com/files/w1441639547/jacob-1.18-x64.rar

放张图片可能表达的更清楚:
在这里插入图片描述

3、整体代码如下

package cn.gx.test;

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

/**
 * 语音播报(window)
 * @author Eric
 * @create 2022-07-16 16:45
 */
public class staticTools {
    
    

    /**【语音播报方法】**/
    public static boolean speakingText(String readText){
    
    
        boolean isFinish = true;
        ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
        try {
    
    
            sap.setProperty("Volume",new Variant(100));              // 音量 0-100
            sap.setProperty("Rate",new Variant(-1));                 // 语音朗读速度 -10 到 +10
            Dispatch sapo = sap.getObject();                         // 获取执行对象
            Dispatch.call(sapo,"Speak",new Variant(readText));    	// 执行朗读
            sapo.safeRelease();                                     // 关闭执行对象
        }catch (Exception e){
    
    
            isFinish = false;
            e.printStackTrace();
        }finally {
    
    
            sap.safeRelease();                                      // 关闭执行对象
        }
        return isFinish;
    }

    public static void main(String[] args) {
    
    
        staticTools.speakingText("您有一笔新的订单,请注意查收");
    }
}

4、测试

测试直接运行 main方法就好了,记得把声音调到最大,感受下成功的喜悦~

备注:播报这个声音完全就是机器人,等有时间会写一篇能在服务器上运行的代码和可以自己选择带感情的声音播报

猜你喜欢

转载自blog.csdn.net/weixin_47316183/article/details/126440096