WEB开发网
开发学院软件开发C语言 C#中实现语音朗读(短信 & 语音短信)sms 阅读

C#中实现语音朗读(短信 & 语音短信)sms

 2009-02-26 08:18:41 来源:WEB开发网   
核心提示: 语言包: http://download.microsoft.com/download/speechSDK/SDK/5.1/WXP/EN-US/speechsdk51LangPack.exe2. 通读 陈本峰 的文章 : .Net平台下开发中文语音应用程序 http://www.micro

语言包: http://download.microsoft.com/download/speechSDK/SDK/5.1/WXP/EN-US/speechsdk51LangPack.exe

2. 通读 陈本峰 的文章 : .Net平台下开发中文语音应用程序  http://www.microsoft.com/china/community/program/originalarticles/TechDoc/Cnspeech.mspx

其中Com组件导入,可以采用Visual Studio的导入Com组件界面完成。

C#中实现语音朗读(短信 & 语音短信)sms

代码如下

代码来自 : 陈本峰

//朗读
private void buttonSynthesis_Click(object sender, System.EventArgs e)
{
    try
{
        SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
        SpVoice Voice = new SpVoice();    
        Voice.Speak(this.textBoxText.Text, SpFlags);
    }
    catch(Exception er)
    {
MessageBox.Show("An Error Occured!","SpeechApp", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}
//生成声音文件(Wav)
private void buttonTTStoWave_Click(object sender, System.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(txtSpeakText.Text, SpFlags);
            Voice.WaitUntilDone(Timeout.Infinite);
             SpFileStream.Close();
        }
    }
    catch(Exception er)
    {
MessageBox.Show("An Error Occured!","SpeechApp", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

3. 采用文章中方法后,我们遇到了问题,系统只能朗读英文,不支持中文朗读。解决方法,定义SpVoice对象的Voice属性为中文类型。

                ISpeechObjectTokens tokens = voice.GetVoices(string.Empty, string.Empty);
                int index = GetChineseVoiceIndex(tokens);
                voice.Voice = tokens.Item(index);

简单的语音短信就生成了。

上一页  1 2 

Tags:实现 语音 朗读

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接