TTS非同步+同步

來源:互聯網
上載者:User

標籤: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();
}
}
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.