C# 语音功能的实现方法

时间:2023-03-15 13:40:51 

首先要安装SpeechSDK5.1 开发包和SpeechSDK5.1 Langague Pack(中英文) 语言包,不过VS2010里是自带SpeechSDK5.0的com组件的,也可以用。

 简单讲一下四个方法:

朗读时,使用


voice.Speak(string,SpeechVoiceSpeakFlags.SVSFlagsAsync);


暂停,使用


voice.Pause();


从暂停中继续刚才的朗读,使用


voice.Resume();


停止功能


voice.Speak(string.Empty, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);


这样就可以完整地实现了“朗读”、“暂停”、“继续”、“停止”的功能。

下面就直接给出实例代码:


 private void button1_Click(object sender, EventArgs e)
        {
            Analyse(this.textBox1.Text);
        }

 

public void Analyse(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 (Convert.ToInt32(chr) <= 122 && Convert.ToInt32(chr) >= 65)
                    {
                        int iLen = i - iCbeg;
                        string strValue =
             strSpeak.Substring(iCbeg, iLen);
                        SpeakChina(strValue);
                        iEbeg = i;
                        IsChina = false;
                    }
                }
                else
                {
                    if (Convert.ToInt32(chr) > 122 || Convert.ToInt32(chr) < 65)
                    {
                        int iLen = i - iEbeg;
                        string strValue =
strSpeak.Substring(iEbeg, iLen);
                        this.SpeakEnglishi(strValue);
                        iCbeg = i;
                        IsChina = true;
                    }
                }
            }

             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 SpeakChina(string speak)
        {
            voice = new SpVoice();
            voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(3);//其中3为中文,024为英文
            voice.Speak(speak, SpeechVoiceSpeakFlags.SVSFDefault);

        }
        //英文
        private void SpeakEnglishi(string speak)
        {
            voice = new SpVoice();
            voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0);//其中3为中文,024为英文
            voice.Speak(speak, SpeechVoiceSpeakFlags.SVSFDefault);

        }

 

//保存语音
        private void button2_Click(object sender, EventArgs e)
        {
                 try    
                 {
                     SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
                     SpVoice Voice = new SpVoice();      
                     SaveFileDialog sfd = new SaveFileDialog();     
                     sfd.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";      
                     sfd.Title = "Save to a wave file";      
                     sfd.FilterIndex = 2;      
                     sfd.RestoreDirectory = true;      
                     if (sfd.ShowDialog() == DialogResult.OK)      
                     {        
                         SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
                         SpFileStream SpFileStream = new SpFileStream();
                         SpFileStream.Open(sfd.FileName, SpFileMode, false);
                         Voice.AudioOutputStream = SpFileStream;
                         Voice.Speak(this.textBox1.Text, SpFlags);
                         Voice.WaitUntilDone(100);
                         SpFileStream.Close();
                     }    
                 }  
                 catch (Exception er)
                 {
                     MessageBox.Show("An Error Occured!", "SpeechApp", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 }


        }


标签:语音功能
0
投稿

猜你喜欢

  • Java文本文件操作方法实例详解

    2021-12-14 10:03:21
  • 详解Android App中创建ViewPager组件的方法

    2023-07-12 00:46:14
  • 关于QueryWrapper,实现MybatisPlus多表关联查询方式

    2021-10-15 07:24:59
  • Java安全-ClassLoader

    2023-08-18 02:12:21
  • 详解java中static关键词的作用

    2023-12-02 00:32:00
  • Android中Retrofit 2.0直接使用JSON进行数据交互

    2022-01-08 07:22:46
  • 史上最简洁C# 生成条形码图片思路及示例分享

    2023-08-24 15:50:25
  • Mybatis防止sql注入原理分析

    2023-08-09 22:54:44
  • C#开发WPF程序中的弱事件模式

    2022-11-21 12:50:34
  • java8中的默认垃圾回收器(GC)

    2021-12-01 04:27:30
  • 掌握Android Handler消息机制核心代码

    2023-11-29 18:29:23
  • Java Boolean 初始化方式详解

    2021-10-09 15:20:07
  • Java实现身份证号码验证源码示例分享

    2022-12-24 03:58:50
  • Android实现底部支付弹窗效果

    2022-06-12 13:39:43
  • Android用PopupWindow实现自定义Dailog

    2023-11-03 13:34:57
  • Android GestureDetector用户手势检测实例讲解

    2022-12-28 17:24:46
  • JAVA 中Spring的@Async用法总结

    2023-11-28 16:35:58
  • idea右键没有java class选项问题解决方案

    2023-05-29 23:38:40
  • 轻松掌握Java观察者模式

    2023-10-10 22:34:54
  • Java实现的日历功能完整示例

    2021-10-12 18:30:21
  • asp之家 软件编程 m.aspxhome.com