java 文字转声音 jacob-1.18

249 阅读1分钟

目录

测试示例

依赖

系统配置

引入

java代码


测试示例

依赖

<dependency>
			<groupId>com.hynnet</groupId>
			<artifactId>jacob</artifactId>
			<version>1.18</version> <!-- 注:如提示报错,先升级基础包版,无法解决可联系技术支持 -->
		</dependency>

 

系统配置

  1.      jacob-1.18-x64.dll 放到C:\Windows\System32
  2.         jacob-1.18-x86.dll 放到C:\Windows\SysWOW64

 

引入

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

 

java代码

package com.superman.test;

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

/**
 * 
 * @author yushen
 *
 */
public class testSY {

	/**
	 * 
	 * <dependency>
			<groupId>com.hynnet</groupId>
			<artifactId>jacob</artifactId>
			<version>1.18</version> <!-- 注:如提示报错,先升级基础包版,无法解决可联系技术支持 -->
		</dependency>
	 * jacob-1.18-x64.dll 放到C:\Windows\System32

		jacob-1.18-x86.dll 放到C:\Windows\SysWOW64

 
 
	 * @param args
	 */
	public static void main(String[] args) {
	    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("小老弟,来玩啊!"));

            System.out.println("声音播放成功!");
	    } catch (Exception e) {
	        e.printStackTrace();
	    } finally {
	        sapo.safeRelease();
	        sap.safeRelease();
	    }
	}

}

 

 

ok