win7系统调用tts的语音朗读功能

windows的tts组件,正版系统或者win10系统是可以直接调用成功的,但win7有的是阉割版,有的不支持,调用的时候各种异常,网上的人各种抄袭,不知道所以然,还爱瞎bb,我就费了好大劲才完全搞清楚。
常见的异常有:

System.IO.FileNotFoundException
  HResult=0x8007007E
  Message=检索 COM 类工厂中 CLSID 为 {D9F6EE60-58C9-458B-88E1-2F908FD7F87C} 的组件失败,原因是出现以下错误: 8007007e 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。
  Source=System.Speech
  StackTrace:
   在 System.Speech.Internal.ObjectTokens.RegistryDataKey..ctor(String fullPath, IntPtr regHandle)
   在 System.Speech.Internal.ObjectTokens.RegistryDataKey.Open(String registryPath, Boolean fCreateIfNotExist)
   在 System.Speech.Internal.ObjectTokens.SAPICategories.DefaultDeviceOut()
   在 System.Speech.Internal.Synthesis.VoiceSynthesis..ctor(WeakReference speechSynthesizer)
   在 System.Speech.Synthesis.SpeechSynthesizer.get_VoiceSynthesizer()
   在 System.Speech.Synthesis.SpeechSynthesizer.SpeakAsync(Prompt prompt)
   在 System.Speech.Synthesis.SpeechSynthesizer.SpeakAsync(String textToSpeak)  
SampleTTSVoice,System.ArgumentException: 不能设置语音。未安装匹配的语音,或语音被禁用。
   在 System.Speech.Synthesis.SpeechSynthesizer.SelectVoice(String name)

开发语言可以使用C#,java,Python,或者微软的VBS,都可以,这几种语言的朗读代码网上都很好找,主要就是配置windows环境。
https://download.csdn.net/download/weixin_42209881/12844165
这是win7的tts修复包,下载下来直接安装,很简单。
修复好之后,就可以进行代码开发了,我以java为例,C#的和Python的可以留言,谁需要,找我要:
先下载一个这个jar包 : jacob.jar在这里插入图片描述
再下载一个dll库(32位和64位的注意区分):jacob-1.17-M2-x64.dll
在这里插入图片描述
这个dll库下载好之后要放在jdk和jre的bin目录下(jre的bin目录放不放不影响,但放上稳妥),就像这样
在这里插入图片描述

这些所有依赖我都放在了一起,可以一次下载下来:
在这里插入图片描述
下载链接:
https://download.csdn.net/download/weixin_42209881/12857855
然后看java代码:

package com.zhang.test.speak;

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

public class SpeakVoice {

	
	public static void main(String[] args) {
		System.out.println(args[0]);
		speak(args[0]);
		//strat("语音朗读的内容", 0);
		System.exit(1);
	}

		/**
		 * 
		* @Title: strat
		* @Description: 该方法的主要作用:朗读
		* @param  @param content
		* @param  @param type 设定文件   0:开始,1停止
		* @return  返回类型:void   
		* @throws
		 */
		public static void strat(String content, int type) {
			//这个Sapi.SpVoice是需要安装什么东西吗,感觉平白无故就来了
			ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
			// Dispatch是做什么的?
			Dispatch sapo = sap.getObject();
			
			if (type == 0) {
				try {
					// 音量 0-100
					sap.setProperty("Volume", new Variant(100));
					// 语音朗读速度 -10 到 +10
					sap.setProperty("Rate", new Variant(1.3));
					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(content));
	 
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					sapo.safeRelease();
					sap.safeRelease();
				}
			} else {
				// 停止
	 
				try {
					Dispatch.call(sapo, "Speak", new Variant(content), new Variant(
							2));
				} catch (Exception e) {
					System.out.println(e.getMessage());
					e.printStackTrace();
				}
			}
	 
		}
		
		
		public static void speak(String text) {
	         ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");  
	            try {  
	                // 音量 0-100  
	                sap.setProperty("Volume", new Variant(100));  
	                // 语音朗读速度 -10 到 +10  
	                sap.setProperty("Rate", new Variant(0));  
	                // 获取执行对象  
	                Dispatch sapo = sap.getObject();  
	                // 执行朗读  
	                Dispatch.call(sapo, "Speak", new Variant(text));  
	                // 关闭执行对象  
	                sapo.safeRelease();  
	            } catch (Exception e) {  
	                e.printStackTrace();  
	            } finally {  
	                // 关闭应用程序连接  
	                sap.safeRelease();  
	            }  
	    }

}

那个下载包里的speak.jar就是我这些代码生成的工具jar包,可以直接调用:
在这里插入图片描述
像这样,运行后,就听到响亮的朗读声音了
有什么不到之处,欢迎大家下方留言,讨论

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 18
    评论
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值