LIVE

导航

用C#实现语音技术

  “电脑朗读”(英文)一个很好的触发点,通过它可以实现电子小说阅读、英文听力测试、英文单词学习...

  下面的Speech已对MSTTS作了简单封装。

  1.安装好MSTTS(如果你有装金山词霸,系统就已经安装了),可以在winnt\speech中打到vtxtauto.tlb文件;

  2.用.Net SDK自带的tlbimp工具把vtxtauto.tlb转换成.dll格式:

  tlbimp vtxtauto.tlb /silent /namespace:mstts /out:mstts.dll
  这时的mstts.dll已成为.net framework运行库的一个类。

  3.编写一个封装vtxtauto的简单类:Speech .

//========================Speech.cs======================
using System;
using mstts; //MSTTS名称空间
namespace Bedlang//定义名称空间
public class Speech{
private VTxtAuto VTxtAutoEx;
public Speech(){
VTxtAutoEx 
= new VTxtAuto(); 
VTxtAutoEx.Register(
" "," "); //注册COM组件 
}

public void Speak(String text){
VTxtAutoEx.Speak(text, 
0); //发音
}

}

}

//========================Speech.cs======================

  4.编译Bedlang.Speech

  csc /target:library /out:Bedlang.dll speech.cs /r:mstts.dll
  如果用vs.net开发,可直接生成项目就可以了。

  5.发音实现

//========================demo.cs======================
分别加入Label,TextBox,Button控件各一个到windows Form中,修改它们的属性,源代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Bedlang
{
/// 
/// Form1 的摘要说明。
/// 

public class demo : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
/// 
/// 必需的设计器变量。
/// 

private System.ComponentModel.Container components = null
public demo()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// 
/// 清理所有正在使用的资源。
/// 

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}

}

base.Dispose( disposing );
}

Windows Form Designer generated code
/// 
/// 应用程序的主入口点。
/// 

[STAThread]
static void Main() 
{
Application.Run(
new demo());
}

private void button1_Click(object sender, System.EventArgs e)
{
Speech s
=new Speech(); //创建一个Speech对象
if(textBox1.Text.Length==0
s.Speak(
"Please input letter."); //发音
else
s.Speak(textBox1.Text);
}

}

}

//========================demo.cs======================

  6.编译demo.cs

  csc demo.cs /r:bedlang.dll

  Vs.net环境下可直接编译成exe文件。

  7.运行demo.exe

  输入要要朗读的文字,程序就可朗读了啦.

posted on 2005-11-17 18:56  2017_LIVE  阅读(341)  评论(0)    收藏  举报