以下代码显示了如何在C#中使用语音合成。
【使用C#进行语音合成,让你的应用说话】在类中有一个名为sintetizador的全局变量, 请记住我们需要包含System.Speech.Synthesis。
本示例使用Async方法, 你将学习如何在不锁定UI的情况下使用监听器(开始, 结束)执行语音。阅读每个功能的摘要以获取更多说明。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Synthesizer.net{
class SynthesizerUIHelper
{
private SpeechSynthesizer sintetizador = new SpeechSynthesizer();
/// <
summary>
/// Speak a string of text asynchronously (without lock the ui). But we will add support for the events that it triggers.
/// <
/summary>
/// <
param name="content">
<
/param>
public void speak(string content = "")
{
try
{
sintetizador.SelectVoice("Microsoft Irina Desktop");
// First list all the voices of the pc with <
listAvailableVoicesConfigurator>
sintetizador.SpeakProgress += new EventHandler<
SpeakProgressEventArgs>
(synthesizer_SpeakProgress);
sintetizador.SpeakCompleted += new EventHandler<
SpeakCompletedEventArgs>
(synthesizer_SpeakCompleted);
sintetizador.SetOutputToDefaultAudioDevice();
sintetizador.SpeakAsync(content);
}
catch (InvalidOperationException e) { Console.WriteLine(e.Message);
}
}
private void synthesizer_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
{
Console.WriteLine("SpeakProgress: AudioPosition=" + e.AudioPosition + ", \tCharacterPosition=" + e.CharacterPosition + ", \tCharacterCount=" + e.CharacterCount + ", \tText=" + e.Text);
}
private void synthesizer_SpeakProgress(object sender, SpeakProgressEventArgs e)
{
Console.WriteLine("SpeakProgress: AudioPosition=" + e.AudioPosition + ", \tCharacterPosition=" + e.CharacterPosition + ", \tCharacterCount=" + e.CharacterCount + ", \tText=" + e.Text);
}
/// <
summary>
/// Stop the previous async speech
/// <
/summary>
public void stop()
{
try
{
sintetizador.SpeakAsyncCancelAll();
}
catch (ObjectDisposedException) { }
}
/// <
summary>
/// Set the sinthesizer volume with an integer (0 - 100)
/// <
/summary>
/// <
param name="level">
<
/param>
public void setVolume(int level)
{
if (level >
100)
{
sintetizador.Volume = 100;
}
else if (level <
0)
{
sintetizador.Volume = 0;
}
else
{
sintetizador.Volume = level;
}
}
public void resume()
{
sintetizador.Resume();
}
public void pause()
{
sintetizador.Pause();
}
/// <
summary>
/// Send speechSynthesizer output to a .wav file
/// <
/summary>
/// <
param name="content">
<
/param>
public void toWAVFile(string content = "")
{
sintetizador.SelectVoice("Microsoft Irina Desktop");
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
sintetizador.SetOutputToWaveFile(path + "/mySpokenAudio.wav");
sintetizador.Speak(content);
MessageBox.Show("File exported succesfully !", ".wav File succesfully exported", MessageBoxButtons.OK, MessageBoxIcon.Information);
sintetizador.SetOutputToDefaultAudioDevice();
}
/// <
summary>
/// Shows a list of all the voices available in the pc, note that you need to figure out a method to choose the voice in the previous functions.
/// <
/summary>
public void listAvailableVoicesConfigurator()
{
using (sintetizador)
{
foreach (InstalledVoice voice in sintetizador.GetInstalledVoices())
{
var info = voice.VoiceInfo;
Console.WriteLine(info.name + " - " +info.Culture);
}
}
}
}}
这个github项目是一个使用cefsharp和.NET Framework 4.5构建的简单应用程序, 它是一个简单的UI, 可让你选择预先安装的Windows语音, 交谈, 暂停并在进度栏中显示语音的进度, 这是一个不错的开始了解语音合成的工作原理。
推荐阅读
- 什么是以及如何在Symfony2中创建数据转换器
- 如何在Symfony 2中为Doctrine存储库创建自定义方法
- TemplateMonster的Shopify会员资格(以99%的折扣价批量获取高级主题)
- 在Symfony 3中使用dhtmlxScheduler创建事件日历(计划程序)
- composer install,composer update和composer require之间有什么区别
- Codelobster(免费的PHP,HTML,CSS,JavaScript编辑器(IDE))
- node app.use()src表示local,dist表示prod
- Android Studio(添加文件后如何“刷新”())
- Android Grid Layout Cardview项目仅在屏幕顶部可见