NET 语言识别,语音控制操作、语音播报

avatar
作者
猴君
阅读量:0
System.Speech.

》》System.Speech.Synthesis; 语音播报
》》System.Speech.Recognition 语音识别

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Speech.Recognition; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Speech; using System.Speech.Synthesis; namespace WindowsFormsApp1 {     public partial class Form2 : Form     {         SpeechRecognitionEngine recognitionEngine;         public Form2()         {             recognitionEngine = new SpeechRecognitionEngine();             Choices choices = new Choices();             choices.Add(new string[] { "开始", "Start", "Go", "停止", "Stop", "Over" });             GrammarBuilder gb = new GrammarBuilder(choices);             Grammar grm = new Grammar(gb);             recognitionEngine.LoadGrammarAsync(grm);             //音频输入             recognitionEngine.SetInputToDefaultAudioDevice();             recognitionEngine.SpeechRecognized += RecognitionEngine_SpeechRecognized;              InitializeComponent();         }            private void RecognitionEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)         {             string info = e.Result.Text;                        switch (info)             {                 case "开始":                 case "Start":                 case "Go":                     richTextBox1.Text += info;                     break;                 case "停止":                 case "Stop":                 case "Over":                     richTextBox1.Text += info;                     break;             }          }         private void Form2_Load(object sender, EventArgs e)         {             this.btn_StopSpeek.Enabled = false;          }          private void btn_startSpeek_Click(object sender, EventArgs e)         {             this.btn_StopSpeek.Enabled = true;             recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);             this.btn_startSpeek.Enabled = false;         }          private void btn_StopSpeek_Click(object sender, EventArgs e)         {             this.btn_StopSpeek.Enabled = false;             recognitionEngine.RecognizeAsyncStop();             this.btn_startSpeek.Enabled = true;         }          private void button1_Click(object sender, EventArgs e)         {             SpeechSynthesizer sp = new SpeechSynthesizer();             PromptBuilder pb = new PromptBuilder();             pb.AppendText("123");             sp.Speak(pb);         }     } }  

语言播报

            SpeechSynthesizer sp = new SpeechSynthesizer();                         sp.Rate = 1;//语速  -10 到 10 之间             sp.Volume = 50;//音量 (0 到 100)             PromptBuilder pb = new PromptBuilder();             pb.AppendText("123");             sp.Speak(pb); 
获取语言包、异步播报、暂停、停止、继续语言播报、保存音频

》》异步播报,就是不阻塞其它操作

            SpeechSynthesizer sp = new SpeechSynthesizer();                         sp.Rate = 1;//语速  -10 到 10 之间             sp.Volume = 50;//音量 (0 到 100)             PromptBuilder pb = new PromptBuilder();             pb.AppendText("1234564878564135415648145");             //同步播报             //sp.Speak(pb);             //异步播报             sp.SpeakAsync(pb); 

》》获取语言包

           SpeechSynthesizer sp = new SpeechSynthesizer();             foreach (var item in sp.GetInstalledVoices())             {                 this.comboBox1.Items.Add(item.VoiceInfo.Name);             } 

》》 异步播报 暂停、继续

if (sp.State == SynthesizerState.Speaking)             {                 // 正在播报 暂停                 sp.Pause();             }             else if (sp.State == SynthesizerState.Paused)             {                 // 已经 暂停,继续播放                 sp.Resume();             } 

》》停止

       if (sp.State == SynthesizerState.Speaking)             {                 //取消所有排队、异步、语音合成操作。                 sp.SpeakAsyncCancelAll();             } 

》》保存音频

          //使用using才能在结束后自动保存语音文件             using (SpeechSynthesizer sp = new SpeechSynthesizer())             {                 string path = @"D:\\zenvideo\";                 sp.SetOutputToWaveFile(path + "1.wav");                 //这句不会播报的,会把声音生成到1.wav                 sp.Speak("13213213213213");             } 

在这里插入图片描述

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!