using System.Globalization;
using System.Linq;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Threading.Tasks;
namespace Fountain.WinConsole.TextToSpeech
{
/// <summary>
/// 文本转语音
/// </summary>
public class TextToSpeech
{
/// <summary>
/// 文本转语音文件
/// </summary>
/// <param name="rate">设置朗读频率 [范围 -10 至 10] </param>
/// <param name="volume">设置朗读音量 [范围 0 至 100] </param>
/// <param name="speektext">播报文本</param>
public static void SpeakingToFile(int rate, int volume, string speektext)
{
try
{
SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer();
speechSynthesizer.Volume = volume;
speechSynthesizer.Rate = rate;
InstalledVoice installedVoice = speechSynthesizer.GetInstalledVoices(CultureInfo.CurrentCulture).FirstOrDefault();
if (installedVoice != null)
{
speechSynthesizer.SetOutputToWaveFile(@"C:\1.wav");
speechSynthesizer.Speak(speektext);
speechSynthesizer.SetOutputToDefaultAudioDevice();
}
}
catch
{
}
}
}
}
using System;
namespace Fountain.WinConsole.TextToSpeech
{
internal class Program
{
static void Main(string[] args)
{
TextToSpeech.SpeakingToFile(4, 100, "欢迎关注dotNet开发技术分享");
Console.ReadKey();
}
}
}