设为首页 加入收藏

TOP

CS程序和BS程序文字转语音(一)
2019-09-17 16:50:32 】 浏览:34
Tags:程序 文字 语音

    一、项目中一直用到了文字转语音的功能,需求也比较简单,就是将一段报警信息通过语音的方式播放出来,之前一直采用CS客户端,利用微软自带的Speech语音播放库就可以完成,

       1.1 封装winSpedk类代码如下:

namespace Speak
{
    using System;
    using System.Runtime.CompilerServices;
    using System.Speech.Synthesis;
    using System.Threading;
    using SpeechLib;

    public class WinSpeak
    {
        #region 属性
        private SpeechSynthesizer Speak;
        public event ErrorInfo ErrorInfoEvent;
        private Thread thVoice;
        private string strVoiceMsg;
        SpVoice Voice = null;
        private static WinSpeak _intence;
        #endregion

        private WinSpeak()
        {
            Voice = new SpVoice();
        }

        #region 方法
        public static WinSpeak _Init()
        {
            if (_intence == null)
                _intence = new WinSpeak();

            return _intence;
        }
        /// <summary>
        /// 读语音
        /// </summary>
        private void SpeakM()
        {
            try
            {
                if (Speak != null)
                {
                    this.Speak.SpeakAsync(strVoiceMsg);
                }
            }
            catch (Exception exception)
            {
                this.ErrInfo(exception);
            }
        }
        /// <summary>
        /// 异步播放文本语音
        /// </summary>
        /// <param name="msg"></param>
        public void BeginSpeakText(string msg)
        {
            try
            {
                if (Speak != null)
                {
                    Speak.SpeakAsyncCancelAll();
                    Speak.Dispose();
                }

                if (thVoice != null && thVoice.ThreadState == ThreadState.Running)
                {
                    thVoice.Abort();
                }

                Speak = new SpeechSynthesizer();
                Speak.SetOutputToDefaultAudioDevice();

                strVoiceMsg = msg;

                //thVoice = new Thread(new ThreadStart(SpeakM));
                //thVoice.Start();
                Speak.SpeakAsync(msg);

                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            catch (Exception exception)
            {
                this.ErrInfo(exception);
            }
        }
        /// <summary>
        /// 播放文本语音
        /// </summary>
        /// <param name="msg"></param>
        public void SpeakText(string msg)
        {
            try
            {
                this.Speak = new SpeechSynthesizer();
                this.Speak.Speak(msg);
                this.Speak.SetOutputToNull();
                this.Speak.Dispose();
            }
            catch (Exception exception)
            {
                this.ErrInfo(exception);
            }
        }
        /// <summary>
        /// Speech播放文本合成语音
        /// </summary>
        /// <param name="msg"></param>
        public void Speech_SpeakText(string msg)
        {
            try
            {
                if (Voice != null)
                {
                    Voice.Speak(null, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
                    Voice.Speak(msg, SpeechVoiceSpeakFlags.SVSFlagsAsync);
                }
            }
            catch (Exception ex)
            {
                this.ErrInfo(ex);
            }
        }
        /// <summary>
        /// 关闭语音释放资源
        /// </summary>
        public void SpeakClose()
        {
            try
            {
                if (Speak != null)
                {
                    this.Speak.SpeakAsyncCancelAll();
                    this.Speak.Dispose();
                }

                if (Voice != null)
                {
                    Voice.Speak(null, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
                }
            }
            catch (Exception ex)
            {
                cGlobe_Log.Error(cGlobe_Log.GetMethodInfo() + ex.Message);
            }
        }
        /// <summary>
        /// 获取错误信息
        /// </summary>
        /// <param name="str"></param>
        private void ErrInfo(Exception str)
        {
            if (this.ErrorInfoEvent != null)
            {
                this.ErrorInfoEvent(str);
            }
        }
        #endregion

        ~WinSpeak()
        {
            try
            {
                if (Speak != null)
                {
                    this.Speak.SpeakAsyncCancelAll();
                    this.Speak.Dispose();
                }
            }
            catch (Exception exception)
            {
                //this.ErrInfo(exception);
            }
        }
    }
}
View Code

      1.2  调用如下(一个同步播放、一个异步播放):  

   pri
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇NET EF 连接Oracle 的配置方法记录 下一篇App_Code下类无法引用问题

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目