C# 語音功能的實現方法_C#教程

來源:互聯網
上載者:User

首先要安裝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);
                 }


        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.