C# 微软 SAPI.SpVoice 简单使用

您所在的位置:网站首页 voice怎么念 C# 微软 SAPI.SpVoice 简单使用

C# 微软 SAPI.SpVoice 简单使用

2023-08-09 01:05| 来源: 网络整理| 查看: 265

 

网上找了好多资料也没找到多少  支持返回当前朗读的位置 用到了C#的回调函数

 

然后为了方便使用 我多此一举的封装了下

 

没什么技术含量 大牛直接飞过

using System; using System.Collections.Generic; using System.Text; using DotNetSpeech; using System.Threading; using System.IO; namespace SpVoiceDemo { class SpVoiceUtil { SpVoice voice = new DotNetSpeech.SpVoiceClass(); public delegate void CallBack(bool b,int InputWordPosition, int InputWordLength); /// /// 朗读文本 /// /// 要朗读的文本 /// 回调地址 /// 返回bool public bool Speak(string str, CallBack CallBack) { int n = voice.Speak(str, SpeechVoiceSpeakFlags.SVSFlagsAsync); Thread thread = new Thread(new ParameterizedThreadStart(Call)); thread.IsBackground = true; thread.Start((Object)CallBack); return !(n!=1); } /// /// 回调函数线程子程序 /// /// private void Call(Object callBack) { int InputWordLength = 0; //局部_朗读长度 int InputWordPosition = 0; //局部_朗读位置 CallBack CallBack = (CallBack)callBack; while ((int)voice.Status.RunningState != 1) { if (InputWordPosition != voice.Status.InputWordPosition || InputWordLength != voice.Status.InputWordLength) { InputWordPosition = voice.Status.InputWordPosition; InputWordLength = voice.Status.InputWordLength; //回调 CallBack(false, InputWordPosition, InputWordLength); } } CallBack(true, InputWordPosition, InputWordLength); } /// /// 获取语音库 /// /// List public List getDescription() { List list = new List(); DotNetSpeech.ISpeechObjectTokens obj = voice.GetVoices(); int count = obj.Count;//获取语音库总数 for (int i = 0; i < count; i++) { string desc = obj.Item(i).GetDescription(); //遍历语音库 list.Add(desc); } return list; } /// /// 设置当前使用语音库 /// /// bool public bool setDescription(string name) { List list = new List(); DotNetSpeech.ISpeechObjectTokens obj = voice.GetVoices(); int count = obj.Count;//获取语音库总数 bool result = false; for (int i = 0; i < count; i++) { string desc = obj.Item(i).GetDescription(); //遍历语音库 if (desc.Equals(name)) { voice.Voice = obj.Item(i); result = true; } } return result; } /// /// 设置语速 /// /// public void setRate(int n) { voice.Rate = n; } /// /// 设置声音大小 /// /// public void setVolume(int n) { voice.Volume = n; } /// /// 暂停 /// public void Pause() { voice.Pause(); } /// /// 继续 /// public void Resume() { voice.Resume(); } /// /// 停止 /// public void Stop() { voice.Speak(string.Empty, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak); } /// /// 输出WAV /// /// 保存路径 /// 要转换的文本内容 /// public bool WreiteToWAV(string path,string str,SpeechAudioFormatType SpAudioType) { SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite; SpFileStream SpFileStream = new SpFileStream(); SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync; SpAudioFormat SpAudio = new DotNetSpeech.SpAudioFormat(); SpAudio.Type = SpAudioType; SpFileStream.Format = SpAudio; SpFileStream.Open(path, SpFileMode, false); voice.AudioOutputStream = SpFileStream; voice.Speak(str, SpFlags); voice.WaitUntilDone(Timeout.Infinite); SpFileStream.Close(); return File.Exists(path); } } }

 

代码下载地址:http://download.csdn.net/detail/rootsuper/5039830



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3