java 文字转声音 播报 hynnet

·  阅读 186

介绍

需要 dll 这玩意,拷贝到位置

     * jacob-1.18-x64.dll 放到C:\Windows\System32

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

 

pom依赖 

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

 

代码依赖

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
复制代码

 

代码

package com.superman.test;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

/**
 * 
 * @author yushen
 *
 */
public class testSY {
	private final static Log log = LogFactory.getLog(testSY.class);
	/**
	 * 
	 * <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
	 */
	
	static String bf = "北山帝皇,娃哈哈,小老弟,来玩啊!";
	
	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(bf));
	        
	        System.out.println("声音播放成功!");
	    } catch (Exception e) {
	    	e.printStackTrace();
	        log.error(e.toString());
	        
	    } finally {
	        sapo.safeRelease();
	        sap.safeRelease();
	    }
	}

}
复制代码

 

* jacob-1.18-x64.dll

download.csdn.net/download/we…

 

ok

 

 

 

 

 

持续更新

 

 

 

 

 

分类:
后端
标签:
收藏成功!
已添加到「」, 点击更改