DotNetSpeech.dll的使用
語音是人類最自然的交互方式,也是現(xiàn)階段軟件用戶界面發(fā)展的最高目標(biāo)。微軟公司一直積極推動語音技術(shù)的發(fā)展,并且公布了語音開發(fā)平臺Speech SDK幫助開發(fā)人員實現(xiàn)語音應(yīng)用。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using DotNetSpeech;
namespace SpeechApp
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button ButtonSynthesis;
private System.Windows.Forms.Button ButtonTTStoWave;
/// <summary>
/// 必需的設(shè)計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗體設(shè)計器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗體設(shè)計器生成的代碼
/// <summary>
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.ButtonSynthesis = new System.Windows.Forms.Button();
this.ButtonTTStoWave = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(272, 144);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "請輸入要合成的文本";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 24);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(256, 112);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "歡迎光臨Lion互動網(wǎng)絡(luò)";
//
// ButtonSynthesis
//
this.ButtonSynthesis.CausesValidation = false;
this.ButtonSynthesis.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.ButtonSynthesis.Location = new System.Drawing.Point(24, 160);
this.ButtonSynthesis.Name = "ButtonSynthesis";
this.ButtonSynthesis.TabIndex = 1;
this.ButtonSynthesis.Text = "朗 讀";
this.ButtonSynthesis.Click += new System.EventHandler(this.ButtonSynthesis_Click);
//
// ButtonTTStoWave
//
this.ButtonTTStoWave.CausesValidation = false;
this.ButtonTTStoWave.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.ButtonTTStoWave.Location = new System.Drawing.Point(128, 160);
this.ButtonTTStoWave.Name = "ButtonTTStoWave";
this.ButtonTTStoWave.Size = new System.Drawing.Size(136, 23);
this.ButtonTTStoWave.TabIndex = 2;
this.ButtonTTStoWave.Text = "生成聲音文件(WAV)";
this.ButtonTTStoWave.Click += new System.EventHandler(this.ButtonTTStoWave_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 191);
this.Controls.Add(this.ButtonTTStoWave);
this.Controls.Add(this.ButtonSynthesis);
this.Controls.Add(this.groupBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "Form1";
this.Text = "歡迎光臨Lion互動網(wǎng)絡(luò)";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
/// <summary>
/// 朗讀
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSynthesis_Click(object sender, System.EventArgs e)
{
try
{
DotNetSpeech.SpeechVoiceSpeakFlags SSF = DotNetSpeech.SpeechVoiceSpeakFlags.SVSFlagsAsync;
DotNetSpeech.SpVoice vo = new SpVoiceClass();
vo.Speak(this.textBox1.Text,SSF);
}
catch(System.Exception ec)
{
MessageBox.Show(ec.ToString(),"SpeechApp",MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error);
}
}
/// <summary>
/// 生成聲音文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonTTStoWave_Click(object sender, System.EventArgs e)
{
try
{
DotNetSpeech.SpeechVoiceSpeakFlags SSF = DotNetSpeech.SpeechVoiceSpeakFlags.SVSFlagsAsync;
DotNetSpeech.SpVoice vo = new SpVoiceClass();
System.Windows.Forms.SaveFileDialog SFD = new System.Windows.Forms.SaveFileDialog();
SFD.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";
SFD.Title = "Save to a wav file";
SFD.FilterIndex = 2;
SFD.RestoreDirectory = true;
if(SFD.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
DotNetSpeech.SpeechStreamFileMode SSFM = DotNetSpeech.SpeechStreamFileMode.SSFMCreateForWrite;
DotNetSpeech.SpFileStream SFS = new DotNetSpeech.SpFileStreamClass();
SFS.Open(SFD.FileName,SSFM,false);
vo.AudioOutputStream = SFS;
vo.Speak(this.textBox1.Text,SSF);
vo.WaitUntilDone(System.Threading.Timeout.Infinite);
SFS.Close();
}
}
catch(System.Exception ec)
{
MessageBox.Show(ec.ToString(),"SpeechApp",MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error);
}
}
}
}
- PC官方版
- 安卓官方手機(jī)版
- IOS官方手機(jī)版