用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 );

}

#region Windows Form Designer generated code

///

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

///

private void InitializeComponent()

{

this.label1 = new System.Windows.Forms.Label();

this.textBox1 = new System.Windows.Forms.TextBox();

this.button1 = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// label1

//

this.label1.Location = new System.Drawing.Point(24, 16);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(120, 23);

this.label1.TabIndex = 0;

this.label1.Text = "输入要朗读的文字:";

//

// textBox1

//

this.textBox1.Location = new System.Drawing.Point(24, 48);

this.textBox1.Name = "textBox1";

this.textBox1.Size = new System.Drawing.Size(248, 21);

this.textBox1.TabIndex = 1;

this.textBox1.Text = "";

//

// button1

//

this.button1.Location = new System.Drawing.Point(112, 112);

this.button1.Name = "button1";

this.button1.TabIndex = 2;

this.button1.Text = "朗读";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// demo

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.ClientSize = new System.Drawing.Size(292, 197);

this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.button1,

this.textBox1,

this.label1});

this.Name = "demo";

this.Text = "demo";

this.ResumeLayout(false);

}

#endregion

///

/// 应用程序的主入口点。

///

[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 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
或许不是做很好,请不要见怪 基本用法: 按自己的意愿布置明天所要背下来的单词,然后到了明天后当自己觉得已经背下来了,则可以使用本程序选择当天的任务(前天布置的)去测试一下背的效果。 本程序的主要特点: 1, 开机自动启动 通过修改系统注册表实现的开机自动运行 2, 会提示昨天是否有布置任务 通过一个标志变量(储存于数据库)来判断当天是否有任务,确切的说应该是当作业布置完以后标志变量就被激活(为“1”的状态是激活的状态,为“0”时则未被激活)则在主人登入界面上会提示是否有任务。 3, 定时自动关闭程序 当程序启动后,会有20秒的时间让主人登入,否则超过了20秒后,程序自动关闭 4, 自动统计答对的题数和正确率 通过一些变量控制来实现对其自动统计 5, 能将正确的和错误的单词明确的指出存放于TextBox 用两个变量分别实现,一个变量储存对字符串,另一个则储存错的字符串,最后根据触发事件分别显示到TextBox中 6, 按钮同时具备显示和隐藏控件的效果 两个按钮“答对的单词”和“答错的单词”分别用于显示对的和错的单词,按一下显示TextBox,按第二下则会隐藏TextBox 7, 使用Engter键代替鼠标点击(确定按钮)或锁定控件焦点 做这个功能主要用于方便文字的输入,每个窗体都具备此功能。尤其是在布置任务的窗体内更需要此功能 附:本程序的初始密码为“123”
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值