您的位置:首页 > 健康 > 养生 > NET 语言识别,语音控制操作、语音播报

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

2024/10/4 0:37:59 来源:https://blog.csdn.net/u013400314/article/details/140550964  浏览:    关键词:NET 语言识别,语音控制操作、语音播报
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.wavsp.Speak("13213213213213");}

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com