文件大小:664.32 KB
文件类型:rar
发布时间:2011-12-29 19:28:21
需资源分:1
下载次数:7
Tag:sapi tts
::资源简介::
英语朗读源码
//=============================================================================
//
// Copyright (c) Microsoft Corporation All Rights Reserved.
//
//=============================================================================
namespace SimpleTTS
{
using System;
using System.Windows.Forms;
using System.Threading;
using SpeechLib;
/// <summary>
/// This form shows how TTS speak works.
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.CheckBox chkSaveToWavFile;
private System.Windows.Forms.Button btnSpeak;
private System.Windows.Forms.TextBox txtSpeakText;
private System.Windows.Forms.Button btnExit;
private System.ComponentModel.IContainer components;
public MainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnSpeak = new System.Windows.Forms.Button();
this.chkSaveToWavFile = new System.Windows.Forms.CheckBox();
this.btnExit = new System.Windows.Forms.Button();
this.txtSpeakText = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// btnSpeak
//
this.btnSpeak.Location = new System.Drawing.Point(130, 86);
this.btnSpeak.Name = "btnSpeak";
this.btnSpeak.Size = new System.Drawing.Size(96, 26);
this.btnSpeak.TabIndex = 4;
this.btnSpeak.Text = "&Speak";
this.btnSpeak.Click += new System.EventHandler(this.btnSpeak_Click);
//
// chkSaveToWavFile
//
this.chkSaveToWavFile.Location = new System.Drawing.Point(10, 86);
this.chkSaveToWavFile.Name = "chkSaveToWavFile";
this.chkSaveToWavFile.Size = new System.Drawing.Size(108, 26);
this.chkSaveToWavFile.TabIndex = 5;
this.chkSaveToWavFile.Text = "Save to .wav";
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(240, 86);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(96, 26);
this.btnExit.TabIndex = 2;
this.btnExit.Text = "&Exit";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// txtSpeakText
//
this.txtSpeakText.Location = new System.Drawing.Point(10, 9);
this.txtSpeakText.Multiline = true;
this.txtSpeakText.Name = "txtSpeakText";
this.txtSpeakText.Size = new System.Drawing.Size(340, 64);
this.txtSpeakText.TabIndex = 3;
this.txtSpeakText.Text = "This is Simple TTS application.";
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(299, 120);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnSpeak);
this.Controls.Add(this.txtSpeakText);
this.Controls.Add(this.chkSaveToWavFile);
this.MaximizeBox = false;
this.Name = "MainForm";
this.Text = "SimpleTTS";
this.Load += new System.EventHandler(this.MainForm_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}
private void btnSpeak_Click(object sender, System.EventArgs e)
{
//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
{
Voice.Speak(txtSpeakText.Text, SpFlags);
}
}
catch(Exception error)
{
MessageBox.Show("Speak error", "SimpleTTS", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnExit_Click(object sender, System.EventArgs e)
{
this.Dispose();
}
private void MainForm_Load(object sender, EventArgs e)
{
}
}
}
::下载地址::
>> 评论