当前位置:编程学习 > C#/ASP.NET >>

C#开发语音程序入门


   要想用C#开发语音程序,首先要电脑上要有Speech API(SPAI)。

大家通过读这段内容可以了解到自己的电脑是否已经安装SPAI:

The Speech API has been an integral component of all Microsoft Windows

versions since Windows 98. Microsoft Windows XP and Windows Server 2003

include SAPI version 5.1. Windows Vista and Windows Server 2008 include SAPI

version 5.3, while Windows 7 includes SAPI version 5.4. Code written for SAPI
 5.3 (Vista)

will run on SAPI 5.4 (Windows 7) without recompiling.

在5.1及以上版本都是支持中、日、英三种语言的。

我们还是先来看个例子,结合小例子再来解释:

新建项目-windows窗体应用程序,名称SpeechDemo

如图:

 


由于我的电脑是win7,所以可以直接添加引用:如图:

 

窗体代码:


[csharp] 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using SpeechLib; 
 
namespace SpeechDemo 

    public partial class Form1 : Form 
    { 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
 
        private void btnSpeech_Click(object sender, EventArgs e) 
        { 
            SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync; 
            SpVoice Voice = new SpVoice(); 
            Voice.Speak(textBox1.Text, SpFlags); 
             
        } 
    } 

代码很简单,只要运行在文本框中输入中文或者英语都可以正常的朗读。

 

 


关于SpVoice接口下的函数可以参见:http://msdn.microsoft.com/en-us/library/ee413476(v=vs.85)

 


相关帮助连接:http://www.microsoft.com/en-us/Tellme/developers/default.aspx?tab=desktop


    可以看到,在text-to-speech engine interface(api-level)下有很多的接口,很多的东西也是让我们摸不着头脑,这时候,我们应该仔细地分析,找到我们所需要的东西,看懂我们要调用的函数、接口,不要被那些我们不熟悉的东西所吓倒,学会即学式+顺便学习的方法,这一点我认为很重要的。
作者:yjjm1990

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,