首页 > 代码库 > TTS异步+同步
TTS异步+同步
异步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();
}
}
}
}