【开发实例】C#调用SAPI实现语音合成的两种方法
- public class Speach
- {
- private static Speach _Instance = null ;
- private SpeechLib.SpVoiceClass voice =null; //SAPI5.1
- private SpeechLib.SpVoice voice = null;//SAPI 5.4
- private Speach()
- {
- BuildSpeach() ;
- }
- public static Speach instance()
- {
- if (_Instance == null)
- _Instance = new Speach() ;
- return _Instance ;
- }
- private void SetChinaVoice()
- {
- voice.Voice = voice.GetVoices(string.Empty,string.Empty).Item(0) ;
- }
- private void SetEnglishVoice()
- {
- voice.Voice = voice.GetVoices(string.Empty,string.Empty).Item(1) ;
- }
- private void SpeakChina(string strSpeak)
- {
- SetChinaVoice() ;
- Speak(strSpeak) ;
- }
- private void SpeakEnglishi(string strSpeak)
- {
- SetEnglishVoice() ;
- Speak(strSpeak) ;
- }
- public void AnalyseSpeak(string strSpeak)
- {
- int iCbeg = 0 ;
- int iEbeg = 0 ;
- bool IsChina = true ;
- for(int i=0;i<strSpeak.Length;i++)
- {
- char chr = strSpeak[i] ;
- if (IsChina)
- {
- if (chr<=122&&chr>=65)
- {
- int iLen = i - iCbeg ;
- string strValue = strSpeak.Substring(iCbeg,iLen) ;
- SpeakChina(strValue) ;
- iEbeg = i ;
- IsChina = false ;
- }
- }
- else
- {
- if (chr>122||chr<65)
- {
- int iLen = i - iEbeg ;
- string strValue = strSpeak.Substring(iEbeg,iLen) ;
- this.SpeakEnglishi(strValue) ;
- iCbeg = i ;
- IsChina = true ;
- }
- }
- }//end for
- if (IsChina)
- {
- int iLen = strSpeak.Length - iCbeg ;
- string strValue = strSpeak.Substring(iCbeg,iLen) ;
- SpeakChina(strValue) ;
- }
- else
- {
- int iLen = strSpeak.Length - iEbeg ;
- string strValue = strSpeak.Substring(iEbeg,iLen) ;
- SpeakEnglishi(strValue) ;
- }
- }
- private void BuildSpeach()
- {
- if (voice == null)
- voice = new SpVoiceClass() ;
- }
- public int Volume
- {
- get
- {
- return voice.Volume ;
- }
- set
- {
- voice.SetVolume((ushort)(value)) ;
- }
- }
- public int Rate
- {
- get
- {
- return voice.Rate ;
- }
- set
- {
- voice.SetRate(value) ;
- }
- }
- private void Speak(string strSpeack)
- {
- try
- {
- voice.Speak(strSpeack,SpeechVoiceSpeakFlags.SVSFlagsAsync) ;
- }
- catch(Exception err)
- {
- throw(new Exception("发生一个错误:"+err.Message)) ;
- }
- }
- public void Stop()
- {
- voice.Speak(string.Empty,SpeechLib.SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak) ;
- }
- public void Pause()
- {
- voice.Pause() ;
- }
- public void Continue()
- {
- voice.Resume() ;
- }
- }//end class
在 private SpeechLib.SpVoiceClass voice =null;这里,我们定义个一个用来发音的类,并且在第一次调用该类时,对它用BuildSpeach方法进行了初始化。
我们还定义了两个属性Volume和Rate,能够设置音量和语速。
我们知道,SpVoiceClass 有一个Speak方法,我们发音主要就是给他传递一个字符串,它负责读出该字符串,如下所示。
- private void Speak(string strSpeack)
- {
- try
- {
- voice.Speak(strSpeack,SpeechVoiceSpeakFlags.SVSFlagsAsync) ;
- }
- catch(Exception err)
- {
- throw(new Exception("发生一个错误:"+err.Message)) ;
- }
- }
第二种使用.NET类库和系统API的代码如下:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Speech.Synthesis;
- using System.Speech;
- namespace StudyBeta
- {
- public class SRead
- {
- public SpeechSynthesizer synth; //语音合成对象
- public SRead()
- {
- synth = new SpeechSynthesizer();
- }
- public SRead(int m, int n)
- {
- //使用 synth 设置朗读音量 [范围 0 ~ 100]
- synth.Volume = m;
- //使用 synth 设置朗读频率 [范围 -10 ~ 10]
- synth.Rate = n;
- }
- public void SpeakChina(string ggg)
- {
- //SpVoice Voice = new SpVoice();
- synth.SelectVoice("Microsoft Lili");
- //Voice.Speak(ggg, SpFlags);
- synth.SpeakAsync(ggg);
- //String speechPeople = synth.Voice;
- //使用 synth 设置朗读音量 [范围 0 ~ 100]
- // synth.Volume = 80;
- //使用 synth 设置朗读频率 [范围 -10 ~ 10]
- // synth.Rate = 0;
- //使用synth 合成 wav 音频文件:
- //synth.SetOutputToWaveFile(string path);
- }
- public void SpeakEnglish(string ggg)
- {
- //SpVoice Voice = new SpVoice();
- synth.SelectVoice("VW Julie");
- synth.Speak(ggg); //ggg为要合成的内容
- }
- public int m
- {
- get
- {
- return synth.Volume;
- }
- set
- {
- synth.Volume = value;
- }
- }
- public int n
- {
- get
- {
- return synth.Rate;
- }
- set
- {
- synth.Rate = value;
- }
- }
- }
【开发实例】C#调用SAPI实现语音合成的两种方法的更多相关文章
- 织梦首页、列表页调用文章body内容的两种方法
http://blog.csdn.net/langyu1021/article/details/52261411 关于首页.列表页调用文章body内容的两种方法,具体方法如下: 第一种方法: {ded ...
- ant中调用外部ant任务的两种方法
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- ios开发——实用技术OC篇》倒计时实现的两种方法
倒计时实现的两种方法 timeFireMethod函数,timeFireMethod进行倒计时的一些操作,完成时把timer给invalidate掉就ok了,代码如下: secondsCountDow ...
- 【Win 10 应用开发】将墨迹保存到图像的两种方法
IT界最近这几年,各种乱七八糟的东西不断出现,其中能用在实际工作与生活中的,大概也就那么几个.Web 前端也冒出各种框架,这就为那些喜欢乱用框架的公司提供了很好的机会,于是造成很多项目体积越来越庞大, ...
- [ARM-Linux开发]Linux下加载.ko驱动模块的两种方法:insmod与modprobe
假设要加载的驱动程序模块名为SHT21.ko 加载驱动模块 方法一: 进入SHT21.ko驱动模块文件所在的目录,然后直接 insmod SHT21.ko 即可 方法二: 将SHT21.ko文 ...
- C# 调用WCF服务的两种方法
项目简介 之前领导布置一个做单点登录的功能给我,实际上就是医院想做一个统一的平台来实现在这个统一的平台登录后不需要在His.Emr.Lis等系统一个个登录,直接可以登录到对应的系统,然后进行相应的操作 ...
- [转]Delphi调用cmd的两种方法
delphi调用cmd的两种方法vars:string;begins:='cmd.exe /c '+edit1.Text+' >c:\1.txt';winexec(pchar(s),sw_hid ...
- C++调用DLL有两种方法——静态调用和动态调用
C++调用DLL有两种方法——静态调用和动态调用 标签: dllc++winapinullc 2011-09-09 09:49 11609人阅读 评论(0) 收藏 举报 分类: cpp(30) [ ...
- 转载]PhpCms V9调用指定栏目子栏目文章的两种方法
PhpCms V9调用指定栏目子栏目文章的两种方法 第一种.直接写子栏目id ,用cat in {pc:get sql="SELECT * from v9_news where status ...
随机推荐
- Independence独立
Independence refers to the degree to which each test case stands alone. That is, does the success or ...
- WebApi参数传递
c# webapi的参数传递方式:1.查询字符串(query string):2.内容主体(content body) 当然也有cookie或url部分或头部信息(header)等其它传方式,这里仅讨 ...
- android参考
android:使用BaseExpandableListAdapter实现可折叠的列表 Android-ListView实现SectionIndexer SectionIndexer 的使用(联系人分 ...
- Redis批量导入数据
首先准备数据文件 格式为 SET Key0 Value0 SET Key1 Value1 ... SET KeyN ValueN 利用shell转换数据 #!/bin/bash while read ...
- Flex的基础用法【转】
//获得屏幕的分辨率 var x:Number=Capabilities.screenResolutionX; var y:Number=Capabilities.screenResolutionY; ...
- proguard 混淆android代码
官网 http://proguard.sourceforge.net/#manual/examples.html android 2.3后,新建的project默认就有一个project.proper ...
- SCAU 10893 Spiral
10893 Spiral 时间限制:1000MS 内存限制:65535K 题型: 编程题 语言: 无限制 Description Given an odd number n, we can ar ...
- 如何开启多用户同时远程连接(Windows2008 Windows2012)
- 【转】Hive导入10G数据的测试
原博文出自于: http://blog.fens.me/hadoop-hive-10g/ 感谢! Hive导入10G数据的测试 让Hadoop跑在云端系列文章,介绍了如何整合虚拟化和Hadoop,让H ...
- C语言中返回字符串函数的四种实现方法 2015-05-17 15:00 23人阅读 评论(0) 收藏
C语言中返回字符串函数的四种实现方法 分类: UNIX/LINUX C/C++ 2010-12-29 02:54 11954人阅读 评论(1) 收藏 举报 语言func存储 有四种方式: 1.使用堆空 ...