请选择 进入手机版 | 继续访问电脑版

大富翁Delphi开发技术网站

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2370|回复: 0

[精品文章] C# Text To Speech

[复制链接]

322

主题

404

帖子

1350

积分

金牌会员

Rank: 6Rank: 6

积分
1350
发表于 2014-6-5 21:19:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
本帖最后由 李子木 于 2014-6-5 21:20 编辑

The .Net Framework makes developing Text to Speech applications very simple with the System.Speech assembly. In this article, I explain how easy it is to get started with Text to Speech.

Let's begin with a console application and add a reference to the System.Speech assembly. Once added you will also need to add theSystem.Speech.Synthesis namespace to your project. The primary class responsible for Text to Speech is the SpeechSynthesizer class. This class takes a "Prompt" and renders it as audio. A Prompt can either be a text or an audio file. Let's take a look at a quick example using the famous "Hello world" as an example text.
  1. Prompt textPrompt = new Prompt("Hello World");  
  2. SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer();  
  3. speechSynthesizer.Speak(textPrompt);
复制代码

Notice the Speak() method of the SpeechSynthesizer class, this method accepts a Prompt and renders it using the default voice. If the default voice is not to your liking, you can change it to another voice. To change the voice, you need to use the SelectVoice method of SpeechSynthesizer class. This method accepts a voice name as its argument. To get a list of available voice names, you can use the GetInstalledVoices() method, which returns a ReadOnlyCollection of typeInstalledVoice. The ReadOnlyCollection can be found in the System.Collections.ObjectModel namespace.

  1. ReadOnlyCollection<InstalledVoice> installedVoice = speechSynthesizer.GetInstalledVoices();  

  2. foreach (InstalledVoice voice in installedVoice)  
  3. {  
  4.     Console.WriteLine(voice.VoiceInfo.Name);  
  5. }
复制代码



The rendered output can easily be saved as a WAV file using the SetOutputToWaveFile() method.

  1. speechSynthesizer.SetOutputToWaveFile("file name.wav");
复制代码

For this last example, let's get the currently spoken word. This is useful if you are developing an application where you want to highlight each word in the text as its spoken. The SpeakProgress event of the SpeechSynthesizer class is raised every time a word is spoken.

  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.          SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer();  
  6.          speechSynthesizer.SpeakProgress += new EventHandler<SpeakProgressEventArgs>(speechSynthesizer_SpeakProgress);  
  7.          speechSynthesizer.Speak("The quick brown fox jumps over the lazy dog");  

  8.          Console.ReadKey();  
  9.     }  

  10.     static void speechSynthesizer_SpeakProgress(object sender, SpeakProgressEventArgs e)  
  11.     {  
  12.          Console.WriteLine(e.Text );  
  13.     }  
  14. }
复制代码




转载请保留当前帖子的链接:http://www.dfwlt.com/forum.php?mod=viewthread&tid=775 谢谢!

搜索简单,分享不易,且分享且珍惜,您难道不想分享下您的心得?如果觉得本文章好的话,您可以在我们网站上注册,然后发布您的好的心得,让大家共同进步,谢谢!

最新添加

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

申请友链|Archiver|手机版|小黑屋|大富翁Delphi开发技术网站 ( 苏ICP备12065705号-4

GMT+8, 2019-10-14 20:26 , Processed in 0.098113 second(s), 28 queries .

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表