using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Speech; using System.Speech.Synthesis; using System.Threading; namespace VoiceTest { public partial class MainWind: Window { public MainWind() { InitializeComponent(); } private SpeechSynthesizer synth = null; /// <summary> /// 返回一个SpeechSynthesizer对象 /// </summary> /// <returns></returns> private SpeechSynthesizer GetSpeechSynthesizerInstance() { if (synth == null) { synth = new SpeechSynthesizer(); } return synth; } /// <summary> /// 播放 /// </summary> public void Play(string text) { Thread thread = new Thread(new ParameterizedThreadStart(SaveAudio)); thread.Start(text); } /// <summary> /// 保存语音文件 /// </summary> /// <param name="text"></param> public void SaveAudio(object text) {
speak(text); string outputFolderPath = "D:\voice"; if(! System.IO.Directory.Exists(outputFolderPath)){ System.IO.Directory.CreateDirectory(outputFolderPath); } synth = GetSpeechSynthesizerInstance(); string spText = text.ToString(); synth.Rate = -2; synth.Volume = 100; string filename = DateTime.Now.ToString("yyyyMMddHHmmss"); string strVoiceFile =System.IO.Path.Combine(outputFolderPath, filename + ".wav"); synth.SetOutputToWaveFile(strVoiceFile); synth.Speak(spText); synth.SetOutputToNull(); //调用语音转文字 //Thread threadVoice = new Thread(VoiceToText); //threadVoice.Start(str); }
public void speak(string speechSounds)
{
SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
SpVoice spVoice = new SpVoice();
spVoice.Rate = spVoice.Rate - 5;
if (spVoice.Volume < 100)
{
spVoice.Volume = spVoice.Volume + 10;
}
if (spVoice.Volume > 9)
{
spVoice.Volume = spVoice.Volume - 10;
}
spVoice.Speak(speechSounds, SpFlags);
}
private void button1_Click(object sender, RoutedEventArgs e) { //Play("hello swimming, today we will learn how to swimming") Play("你好!今天我们将学习如何提高游泳技巧!不用太费力气哦,haha!"); } } }
参考:https://www.cnblogs.com/-maomao/p/6861447.html