电脑朗读”(英文)一个很好的触发点,通过它可以实现电子小说阅读、英文听力测试、英文单词学习...
下面的Speech已对MSTTS作了简单封装。
下面的Speech已对MSTTS作了简单封装。
1.安装好MSTTS(如果你有装金山词霸,系统就已经安装了,在C:\windows\speech\下),可以在winntspeech中打到vtxtauto.tlb文件; 没有的话,就要装TTS和SAPI在金山的碟上有这两个文件!
TTS:Microsoft Text-To-Speech Engine (全文朗读引擎)
SAPI:Microsoft Speech API (语音API)
2.用.NetSDK自带的tlbimp工具把vtxtauto.tlb转换成.dll格式:
tlbimpvtxtauto.tlb/silent/namespace:mstts/out:mstts.dll
这时的mstts.dll已成为.netframework运行库的一个类。
3.编写一个封装vtxtauto的简单类:Speech.
//========================Speech.cs======================
usingSystem;
usingmstts;//MSTTS名称空间
namespaceBedlang{//定义名称空间
publicclassSpeech{
privateVTxtAutoVTxtAutoEx;
publicSpeech(){
VTxtAutoEx=newVTxtAuto();
VTxtAutoEx.Register("","");//注册COM组件
}
publicvoidSpeak(Stringtext){
VTxtAutoEx.Speak(text,0);//发音
}
}
}
//========================Speech.cs======================
4.编译Bedlang.Speech
csc/target:library/out:Bedlang.dllspeech.cs/r:mstts.dll
如果用vs.net开发,可直接生成项目就可以了。
5.发音实现
//========================demo.cs======================
分别加入Label,TextBox,Button控件各一个到windowsForm中,修改它们的属性,源代码如下:
usingSystem;
usingSystem.Drawing;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Windows.Forms;
usingSystem.Data;
namespaceBedlang
{
///
///Form1的摘要说明。
///
publicclassdemo:System.Windows.Forms.Form
{
privateSystem.Windows.Forms.Labellabel1;
privateSystem.Windows.Forms.TextBoxtextBox1;
privateSystem.Windows.Forms.Buttonbutton1;
///
///必需的设计器变量。
///
privateSystem.ComponentModel.Containercomponents=null;
publicdemo()
{
//
//Windows窗体设计器支持所必需的
//
InitializeComponent();
//
//TODO:在InitializeComponent调用后添加任何构造函数代码
//
}
///
///清理所有正在使用的资源。
///
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#regionWindowsFormDesignergeneratedcode
///
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
///
privatevoidInitializeComponent()
{
this.label1=newSystem.Windows.Forms.Label();
this.textBox1=newSystem.Windows.Forms.TextBox();
this.button1=newSystem.Windows.Forms.Button();
this.SuspendLayout();
//
//label1
//
this.label1.Location=newSystem.Drawing.Point(24,16);
this.label1.Name="label1";
this.label1.Size=newSystem.Drawing.Size(120,23);
this.label1.TabIndex=0;
this.label1.Text="输入要朗读的文字:";
//
//textBox1
//
this.textBox1.Location=newSystem.Drawing.Point(24,48);
this.textBox1.Name="textBox1";
this.textBox1.Size=newSystem.Drawing.Size(248,21);
this.textBox1.TabIndex=1;
this.textBox1.Text="";
//
//button1
//
this.button1.Location=newSystem.Drawing.Point(112,112);
this.button1.Name="button1";
this.button1.TabIndex=2;
this.button1.Text="朗读";
this.button1.Click+=newSystem.EventHandler(this.button1_Click);
//
//demo
//
this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);
this.ClientSize=newSystem.Drawing.Size(292,197);
this.Controls.AddRange(newSystem.Windows.Forms.Control[]{
this.button1,
this.textBox1,
this.label1});
this.Name="demo";
this.Text="demo";
this.ResumeLayout(false);
}
#endregion
///
///应用程序的主入口点。
///
[STAThread]
staticvoidMain()
{
Application.Run(newdemo());
}
privatevoidbutton1_Click(objectsender,System.EventArgse)
{
Speechs=newSpeech();//创建一个Speech对象
if(textBox1.Text.Length==0)
s.Speak("Pleaseinputletter."); //发音
else
s.Speak(textBox1.Text);
}
}
}
//========================demo.cs======================
6.编译demo.cs
cscdemo.cs/r:bedlang.dll
Vs.net环境下可直接编译成exe文件。
7.运行demo.exe
输入要要朗读的文字,程序就可朗读了啦.