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

System.Speech.

》》System.Speech.Synthesis; 语音播报

》》System.Speech.Recognition 语音识别

csharp 复制代码
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);
        }
    }
}

语言播报

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

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

csharp 复制代码
            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);

》》获取语言包

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

》》 异步播报 暂停、继续

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

》》停止

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

》》保存音频

csharp 复制代码
          //使用using才能在结束后自动保存语音文件
            using (SpeechSynthesizer sp = new SpeechSynthesizer())
            {
                string path = @"D:\\zenvideo\";
                sp.SetOutputToWaveFile(path + "1.wav");
                //这句不会播报的,会把声音生成到1.wav
                sp.Speak("13213213213213");
            }
相关推荐
子不语3 个月前
c#实现数据导出为PDF的方式
pdf·c#·导出·文档·net
Ares-Wang3 个月前
句 柄 概 念
net
Ares-Wang3 个月前
NET 定时器 Timer和线程Thread
net
一个十几年的老程序员4 个月前
好玩的调度技术-场景编辑器
javascript·c#·net
tonyhi65 个月前
Ubuntu server 24 (Linux) IPtables 双网卡 共享上网NAT 安装配置DHCP
linux·运维·ubuntu·iptables·dhcp·net
它朝若是6 个月前
C#创建netcore配置program文件
c#·net
路过秋天7 个月前
.NET Emit 入门教程:第六部分:IL 指令:8:详解 ILGenerator 指令方法:类型转换指令
emit·net
路过秋天7 个月前
.NET Emit 入门教程:第六部分:IL 指令:7:详解 ILGenerator 指令方法:分支条件指令
emit·net
路过秋天7 个月前
.NET Emit 入门教程:第五部分:动态生成方法(MethodBuilder 与 DynamicMethod)
emit·net·methodbuilder·dynamicmethod