java调jacob包实现系统语音

声明:并非原创

目的:只为记录

1.maven工程;

2.导jar包,https://share.weiyun.com/59jtoxW;

3.代码


/** 
* Project Name:TextToVoice 
* File Name:JacobStreamTask.java 
* Package Name:com.xx.util 
* Date:2018年12月7日上午9:54:46 
* Copyright (c) 2018, Gao All Rights Reserved. 
* 
*/  
 
package com.xx.texttovoice.util;

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

/** 
 * @Desc: 
 * @Auth: 
 * @Date: 2018年12月7日 上午9:54:46
 */

public class TextToVoice {
	
	public static void main(String[] args) {
		trans("这是一条测试信息");
	}

	
	
	public static void trans(String text){
		ActiveXComponent activeXComponent = null;
		try {
//												jacob封装好的可供选择的API
			activeXComponent = new ActiveXComponent("Sapi.SpVoice");
//			运行时输出语音内容
			Dispatch dispatch = activeXComponent.getObject();
//			设置音量
			activeXComponent.setProperty("Volume", new Variant(100));
//			语音的朗读速度-10到 +10
			activeXComponent.setProperty("Rate", new Variant(1));
//			调用执行朗读
			Dispatch.call(dispatch, "Speak",new Variant(text));
			
//			生成语音文件
			activeXComponent = new ActiveXComponent("Sapi.SpFileStream");
			Dispatch fileStreamDispatch = activeXComponent.getObject();
			
//			音频
			activeXComponent = new ActiveXComponent("Sapi.SpAudioFormat");
			Dispatch audioDispatch = activeXComponent.getObject();
			
//			设置文件流格式
			Dispatch.putRef(fileStreamDispatch, "Format", audioDispatch);
//			设置音频流格式
			Dispatch.put(audioDispatch, "Type", new Variant(22));
			
//			调用输出文件流打开方法,创建一个.wav .mp3 .mp4   .wma文件
			Dispatch.call(fileStreamDispatch, "Open", new Variant("src/main/resources/static/test.mp3"),new Variant(3),new Variant(true));
//			设置声音对象的音频流输出流为输出文件对象
			Dispatch.putRef(dispatch, "AudioOutputStream", fileStreamDispatch);
			
//			设置音量0-100
			Dispatch.put(dispatch, "Volume", new Variant(100));
//			设置朗读速度
			Dispatch.put(dispatch, "Rate", new Variant(-2));
//			开始朗读
			Dispatch.call(dispatch, "Speak",new Variant(text));
			
//			关闭输出文件流
			Dispatch.call(fileStreamDispatch, "Close");
			Dispatch.putRef(dispatch, "AudioOutputStream", null);
			
			audioDispatch.safeRelease();
			fileStreamDispatch.safeRelease();
			dispatch.safeRelease();
			
			
			activeXComponent.safeRelease();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	
}

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值