码迷,mamicode.com
首页 > 其他好文 > 详细

TTS异步+同步

时间:2014-08-19 18:36:25      阅读:346      评论:0      收藏:0      [点我收藏+]

标签:io   文件   for   ar   代码   amp   line   new   

异步C#代码:


using System; using System.Speech.Synthesis; namespace SampleSynthesis { class Program { static void Main(string[] args) { // Initialize a new instance of the SpeechSynthesizer. SpeechSynthesizer synth = new SpeechSynthesizer(); // Configure the audio output. synth.SetOutputToWaveFile(@"C:\Test\Sample.wav"); // Register for the SpeakCompleted event. synth.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(synth_SpeakCompleted); // Build a prompt. PromptBuilder builder = new PromptBuilder(); builder.AppendText("This sample asynchronously speaks a prompt to a WAVE file."); // Speak the string asynchronously. synth.SpeakAsync(builder); Console.WriteLine(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } // Handle the SpeakCompleted event. static void synth_SpeakCompleted(object sender, SpeakCompletedEventArgs e) { // Create a SoundPlayer instance to play the output audio file. System.Media.SoundPlayer m_SoundPlayer = new System.Media.SoundPlayer(@"C:\Test\Sample.wav"); // Play the output file. m_SoundPlayer.Play(); } } }


同步C#代码:


using System;
using System.Linq;
using System.Text;
using System.Speech.Synthesis;
using System.Speech.AudioFormat;

namespace SAPITest
{
public class SapiImpl/*:ISapi*/
{
static void Main(string[] args)
{
if (args.Length < 3)
{
Console.WriteLine("Too less Params: " + args.Length);
return;
}

String textContent = args[0];
String path = args[1];
String NameFomat = args[2];
// String textContent = "返回的字符串值,与需要调用的类的方法名一致";
// String path = "D:\\temp\\";
// String NameFomat = "b.avi";

/*Console.WriteLine(textContent + " " + path + " " + NameFomat);*/
mytts(textContent, path, NameFomat);
/* Console.WriteLine("Done...");*/
/*Console.WriteLine("Press any key to exit...");
Console.ReadKey();*/
}

static void mytts(String textContent , String path , String NameFomat)
{
//初始化SpeechSynthesizer实例
using (SpeechSynthesizer synth = new SpeechSynthesizer())
{
// 配置音频输出路径
String savePath = path + NameFomat;
synth.SetOutputToWaveFile(savePath);
//synth.SetOutputToWaveFile(savePath,
// new SpeechAudioFormatInfo(32000, AudioBitsPerSample.Sixteen, AudioChannel.Mono));

// 创建内容
PromptBuilder builder = new PromptBuilder();
builder.AppendText(textContent);

// 输出音频文件
synth.Speak(builder);

// 为输出音频创建播放器实例
/*System.Media.SoundPlayer m_SoundPlayer =
new System.Media.SoundPlayer(savePath);*/
//播放输出的音频文件
//m_SoundPlayer.Play();
}
}
}
}

TTS异步+同步,布布扣,bubuko.com

TTS异步+同步

标签:io   文件   for   ar   代码   amp   line   new   

原文地址:http://www.cnblogs.com/freedesert/p/3922608.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!