C#分享辅助类:朗读文本或生成语音(VoiceHandler)

编程入门 行业动态 更新时间:2024-10-19 14:41:08

C#分享辅助类:朗读文本或生成<a href=https://www.elefans.com/category/jswz/34/1769309.html style=语音(VoiceHandler)"/>

C#分享辅助类:朗读文本或生成语音(VoiceHandler)

名称

方法

朗读声音,播放声音

PlaySound

播放音频文件

PlayWav

保存声音文件

SaveSound

释放资源

Dispose

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Runtime.InteropServices;
using System.Threading;namespace Wsfly
{public class VoiceHandler{const int SpFlags = 1;                          //SpeechVoiceSpeakFlags.SVSFlagsAsynstatic object _spVoiceCls = null;               //保存朗读用的 SAPI.SpVoice static object _oISpeechObjectTokens = null;     //保存 SAPI.ISpeechObjectTokens 就是系统有的语音引擎集合static int _tokensCount = 0;                    //语音引擎集合数public static DictionaryEntry[] _deTokens = null;   //语音模块static string _currentTokenName = null;             //当前语音模块名称/// <summary>/// 当前语音模块/// </summary>public static object CurrentToken{get{//未设置播放语音模块 使用默认if (string.IsNullOrWhiteSpace(_currentTokenName)) return _deTokens[0];//循环得到名称foreach (DictionaryEntry entry in _deTokens){if (entry.Key.ToString() == _currentTokenName){return entry;}}//返回默认return _deTokens[0];}set{//当前语音模块_currentTokenName = value.ToString();}}//是否初始化static bool isInit = false;/// <summary>/// 初始化/// </summary>private VoiceHandler(){InitSAPI();isInit = true;}/// <summary>/// 初始化 API/// </summary>public static void InitSAPI(){//如果已经初始化 则退出if (isInit) return;//创建语音对象朗读用_spVoiceCls = ComHandler.CreateComObject("SAPI.SpVoice");if (_spVoiceCls == null){throw new Exception("您还未安装微软语音插件!");}else{//取得所有的 识别对象模块集合//取得SAPI.ISpeechObjectTokens_oISpeechObjectTokens = ComHandler.CallComMethod("GetVoices", _spVoiceCls);//识别对象集合 Count;object r = ComHandler.GetComPropery("Count", _oISpeechObjectTokens);if (r is int){//语言数量_tokensCount = (int)r;//有语言模块if (_tokensCount > 0){//取得全部语音识别对象模块,及名称,以被以后使用_deTokens = new DictionaryEntry[_tokensCount];for (int i = 0; i < _tokensCount; i++){//从集合中取出单个 识别对象模块object oSpObjectToken = ComHandler.CallComMethod("Item", _oISpeechObjectTokens, i); //返回 SAPI.SpObjectToken//取名称string name = ComHandler.CallComMethod("GetDescription", oSpObjectToken) as string;//放到 DictionaryEntry 对象中,key 是 名称, value 是 识别对象模块_deTokens[i] = new DictionaryEntry(name, oSpObjectToken);}}}}}/// <summary>/// 朗读声音,播放声音/// </summary>/// <param name="msg"></param>public static void PlaySound(string msg, int rate = 0, int volume = 100){///初始化if (!isInit) { new VoiceHandler(); }try{if (_spVoiceCls != null){//设置语言引擎ComHandler.SetComProperty("Voice", _spVoiceCls, ((DictionaryEntry)CurrentToken).Value);ComHandler.SetComProperty("Rate", _spVoiceCls, rate);ComHandler.SetComProperty("Volume", _spVoiceCls, volume);//调用Speak 函数,msg 是要播放的文本,1 是异步播放,因为是异步的 com 对象不立刻释放ComHandler.CallComMethod("Speak", _spVoiceCls, msg, SpFlags);}}catch (Exception ex){}}/// <summary>/// 播放音频文件/// </summary>/// <param name="stream"></param>public static void PlayWav(System.IO.Stream stream){System.Media.SoundPlayer player = new System.Media.SoundPlayer(stream);player.Play();}/// <summary>/// 保存声音文件/// </summary>/// <param name="msg">播放的语音</param>/// <param name="fileName">保存路径</param>/// <param name="rate">语速</param>/// <param name="volume">音量</param>public static void SaveSound(string msg, string fileName, int rate = 0, int volume = 100){int SpFileMode = 3;object oSpFileStream = ComHandler.CreateComObject("SAPI.SpFileStream"); //创建 SAPI.SpFileStreamobject oSpVoice = ComHandler.CreateComObject("SAPI.SpVoice"); //创建 SAPI.SpVoicetry{ComHandler.CallComMethod("Open", oSpFileStream, fileName, SpFileMode, false); //打开流ComHandler.SetComProperty("Voice", oSpVoice, ((DictionaryEntry)CurrentToken).Value); //设置 Voice 属性,让谁朗读ComHandler.SetComProperty("Rate", oSpVoice, rate);ComHandler.SetComProperty("Volume", oSpVoice, volume);ComHandler.SetComProperty("AudioOutputStream", oSpVoice, oSpFileStream); //设置流ComHandler.CallComMethod("Speak", oSpVoice, msg, SpFlags); //调用 SpeakComHandler.CallComMethod("WaitUntilDone", oSpVoice, Timeout.Infinite); //等ComHandler.CallComMethod("Close", oSpFileStream); //关闭流}finally{Marshal.ReleaseComObject(oSpVoice);Marshal.ReleaseComObject(oSpFileStream);}}/// <summary>/// 释放资源/// </summary>public static void Dispose(){//释放com对象Marshal.ReleaseComObject(_spVoiceCls);//标记未初始化isInit = false;}}
}

更多推荐

C#分享辅助类:朗读文本或生成语音(VoiceHandler)

本文发布于:2024-02-06 17:29:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1750359.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语音   文本   VoiceHandler

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!