c#语音朗读类

来源:互联网 发布:开淘宝店要注意哪些 编辑:程序博客网 时间:2024/04/29 22:37

用网上找的资料写的一个类,调用系统语音朗读,要使用中文发音电脑需要安装ChineseTTS,3点多M,网上有很多下载的地方,发音不够真,也可以找更好的语音安装,然后在文中红色的代码中的 Microsoft Simplified Chinese 改为新安装的语音名(可通过取断点查看_LanguageList列表查看当前系统中安装的语音列表)
通过调用 public void Speeking(string strWords)  方法即可实现朗读

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors.Controls;

namespace MyTool.Common
{
    class MyTTS
    {
        object _spVoiceCls = null;    //保存朗读用的 SAPI.SpVoice
        const int SpFlags = 1;  //SpeechVoiceSpeakFlags.SVSFlagsAsyn 
        object _oISpeechObjectTokens = null; //保存 SAPI.ISpeechObjectTokens 就是系统有的语音引擎集合
        int TokensCount ; // 语音引擎集合 数
        //DictionaryEntry[] _deTokens = null;
        private static readonly Hashtable _LanguageList = Hashtable.Synchronized(new Hashtable());
       // private Hashtable _LanguageList = new Hashtable();
        private void InitSAPI()
        {
            //创建语音对象朗读用
            _spVoiceCls = CreateComObject("SAPI.SpVoice");
            if (_spVoiceCls == null)
            {
                MessageBox.Show("您的系统没有,微软语音组件");
                return;
            }
          
            //取得所有的 识别对象模块集合
            _oISpeechObjectTokens = CallComMethod("GetVoices", _spVoiceCls); //取得SAPI.ISpeechObjectTokens
            //识别对象集合 Count;
            object r = GetComPropery("Count", _oISpeechObjectTokens);
            if (r is int)
            {

                TokensCount = (int)r;

                if (TokensCount > 0)
                {
                    //取得全部语音识别对象模块,及名称,以被以后使用
                   // _deTokens = new DictionaryEntry[TokensCount];
                    _LanguageList.Clear();
                    for (int i = 0; i < TokensCount; i++)
                    {
                        //从集合中取出单个 识别对象模块
                        object oSpObjectToken = CallComMethod("Item", _oISpeechObjectTokens, i); //返回 SAPI.SpObjectToken
                        //取名称
                        string description = CallComMethod("GetDescription", oSpObjectToken) as string;
                        //放到 DictionaryEntry 对象中,key 是 识别对象模块,value 是名称
                        //_deTokens = new DictionaryEntry(oSpObjectToken, description);
                        _LanguageList.Add(description, oSpObjectToken);
                    }
                   
                }

            }
        }
        #region 调用com组件,功能通用函数
        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="name"></param>
        /// <param name="o"></param>
        /// <param name="vlaue"></param>
        private static void SetComProperty(string name, object o, object vlaue)
        {
            Type t = o.GetType();
            t.InvokeMember(name, BindingFlags.Instance | BindingFlags.SetProperty, null, o, new
object[] { vlaue });
        }
        /// <summary>
        /// 取得属性
        /// </summary>
        /// <param name="name"></param>
        /// <param name="o"></param>
        /// <returns></returns>
        private static object GetComPropery(string name, object o)
        {
            Type t = o.GetType();
            return t.InvokeMember(name, BindingFlags.Instance | BindingFlags.GetProperty, null, o, null);
        }
        /// <summary>
        /// 调用方法函授
        /// </summary>
        /// <param name="name"></param>
        /// <param name="o"></param>
        /// <param name="parms"></param>
        /// <returns></returns>
        private static object CallComMethod(string name, object o, params object[] parms)
        {
            Type t = o.GetType();

            return t.InvokeMember(name, BindingFlags.Instance | BindingFlags.InvokeMethod, null, o, parms);
        }
        /// <summary>
        /// 创建 com 对象
        /// </summary>
        /// <param name="FromProgID"></param>
        /// <returns></returns>
        private static object CreateComObject(string FromProgID)
        {
            Type comType = Type.GetTypeFromProgID(FromProgID);
            object rVar = null;
            if (comType != null)
                rVar = System.Activator.CreateInstance(comType);

            return rVar;
        }
        #endregion

       public void Speeking(string strWords)
       {
           //if(_LanguageList.Count==0)
           InitSAPI();
           if (strWords.Length==0)
               return;
           if(_spVoiceCls==null)
               return;
            //设置语言引擎
           SetComProperty("Voice", _spVoiceCls, _LanguageList["Microsoft Simplified Chinese"]);
           //调用Speak 函数,1 是异步播放,因为是异步的 com 对象不立刻释放
           CallComMethod("Speak", _spVoiceCls, strWords, SpFlags);
        }
    }
}

原创粉丝点击