当前位置:   article > 正文

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

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);
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82

语言播报

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

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

            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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

》》获取语言包

           SpeechSynthesizer sp = new SpeechSynthesizer();
            foreach (var item in sp.GetInstalledVoices())
            {
                this.comboBox1.Items.Add(item.VoiceInfo.Name);
            }
  • 1
  • 2
  • 3
  • 4
  • 5

》》 异步播报 暂停、继续

if (sp.State == SynthesizerState.Speaking)
            {
                // 正在播报 暂停
                sp.Pause();
            }
            else if (sp.State == SynthesizerState.Paused)
            {
                // 已经 暂停,继续播放
                sp.Resume();
            }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

》》停止

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

》》保存音频

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

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/863135
推荐阅读