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開發(fā)技術分享");
Console.ReadKey();
}
}
}