www.pudn.com > MSTTSSpeech.rar > Form1.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using SpeechLib;
namespace WindowsApplication1
{
///
/// Form1 的摘要说明。
///
public class Form1 : System.Windows.Forms.Form
{
private SpVoice Voice;
SpeechVoiceSpeakFlags SpFlags;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.CheckBox chkSaveToWavFile;
private System.Windows.Forms.TextBox txtSpeakText;
private System.Windows.Forms.ComboBox CB1;
private System.Windows.Forms.TrackBar TB_Rate;
///
/// 必需的设计器变量。
///
private System.ComponentModel.Container components = null;
public Form1()
{
SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
Voice = new SpVoice();
ISpeechObjectTokens voices = Voice.GetVoices("","");
Voice.Voice = voices.Item(voices.Count-1);
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.txtSpeakText = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.chkSaveToWavFile = new System.Windows.Forms.CheckBox();
this.CB1 = new System.Windows.Forms.ComboBox();
this.TB_Rate = new System.Windows.Forms.TrackBar();
((System.ComponentModel.ISupportInitialize)(this.TB_Rate)).BeginInit();
this.SuspendLayout();
//
// txtSpeakText
//
this.txtSpeakText.Location = new System.Drawing.Point(32, 24);
this.txtSpeakText.Multiline = true;
this.txtSpeakText.Name = "txtSpeakText";
this.txtSpeakText.Size = new System.Drawing.Size(296, 112);
this.txtSpeakText.TabIndex = 0;
this.txtSpeakText.Text = "请林光头 到内科第一诊室";
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 168);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// chkSaveToWavFile
//
this.chkSaveToWavFile.Location = new System.Drawing.Point(40, 216);
this.chkSaveToWavFile.Name = "chkSaveToWavFile";
this.chkSaveToWavFile.TabIndex = 3;
this.chkSaveToWavFile.Text = "checkBox1";
//
// CB1
//
this.CB1.Location = new System.Drawing.Point(160, 184);
this.CB1.Name = "CB1";
this.CB1.Size = new System.Drawing.Size(256, 20);
this.CB1.TabIndex = 4;
//
// TB_Rate
//
this.TB_Rate.Location = new System.Drawing.Point(72, 264);
this.TB_Rate.Minimum = -10;
this.TB_Rate.Name = "TB_Rate";
this.TB_Rate.Size = new System.Drawing.Size(272, 42);
this.TB_Rate.SmallChange = 5;
this.TB_Rate.TabIndex = 5;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(440, 317);
this.Controls.Add(this.TB_Rate);
this.Controls.Add(this.CB1);
this.Controls.Add(this.chkSaveToWavFile);
this.Controls.Add(this.button1);
this.Controls.Add(this.txtSpeakText);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.TB_Rate)).EndInit();
this.ResumeLayout(false);
}
#endregion
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
Speak(txtSpeakText.Text);
/*
//Create a TTS voice and speak.
try
{
SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
SpVoice Voice = new SpVoice();
if (chkSaveToWavFile.Checked)
{
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();
}
}
else
{
ISpeechObjectTokens voices = Voice.GetVoices("","");
//CB1.Items.Clear();
//for(int i=0;i