у меня есть проблема в System.speech в c# VB проблема: не установлен распознаватель - PullRequest
0 голосов
/ 02 апреля 2020
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech;
using System.Speech.Synthesis;
using System.Speech.Recognition;


using System.IO;

namespace Ai_speach_test
{
    public partial class Form1 : Form
    {

        SpeechRecognitionEngine _reconizer = new SpeechRecognitionEngine();
        SpeechSynthesizer hana = new SpeechSynthesizer();
        SpeechRecognitionEngine start_listening = new SpeechRecognitionEngine();
        Random rad = new Random();
        int timeout = 0;
        DateTime timeNow = DateTime.Now;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

                // I think the problem is here.

                _reconizer.SetInputToDefaultAudioDevice();
                _reconizer.LoadGrammarAsync(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"Defolt_commands.txt")))));
                _reconizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(Default_SpeechReccpgnized);
                _reconizer.SpeechDetected += new EventHandler<SpeechDetectedEventArgs>(_reconizer_SpeechReccpgnized);
                _reconizer.RecognizeAsync(RecognizeMode.Multiple);
                start_listening.SetInputToDefaultAudioDevice();
                start_listening.LoadGrammarAsync(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"Defolt_commands.txt")))));
                start_listening.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(start_listening_SpeechReccpgnized);

        }

        private void Default_SpeechReccpgnized(object sender, SpeechRecognizedEventArgs e)
        {
            int renNum;
            string speech = e.Result.Text;
            if (speech == " hello") { hana.SpeakAsync("hi  mr mohammed how can i help you "); }
            if (speech == "how are you" || speech == "how is going ") { hana.SpeakAsync("every think is fine if you are fine "); }
            if (speech == "what  time is it") { hana.SpeakAsync(DateTime.Now.ToString("h mm tt")); }

            if (speech == "stop talking" || speech == "hana stop talking")
            {
                hana.SpeakAsyncCancelAll();
                renNum = rad.Next(1, 2);
                if (renNum == 1) { hana.SpeakAsync("yes sir do you have anther command "); }
                if (renNum == 2) { hana.SpeakAsync("alright sir "); }
            }

            if (speech == "stop listening" || speech == "there is no command hana")
            {
                _reconizer.RecognizeAsyncCancel();
                start_listening.RecognizeAsync(RecognizeMode.Multiple);
                renNum = rad.Next(1, 2);
                if (renNum == 1) { hana.SpeakAsync("yes sir if you want me back call me agin"); }
                if (renNum == 2) { hana.SpeakAsync("alright sir if you want me jsut ask "); }

            }
            if (speech == "show commands" || speech == "show options")
            {
                renNum = rad.Next(1, 2);
                if (renNum == 1) { hana.SpeakAsync("yes sir");}
                if (renNum == 2) { hana.SpeakAsync("alright sir ");}
                string[] commands = (File.ReadAllLines(@"Defolt_commands"));
                    listcommands.Items.Clear();
                    listcommands.SelectionMode = SelectionMode.None;
                    listcommands.Visible = true;
                    foreach (string command in commands)
                    {
                        listcommands.Items.Add(command);
                    }
                }
                if (speech == "hide commands " || speech == "hana hide commands")
                {
                    renNum = rad.Next(1, 2);
                    if (renNum == 1) { hana.SpeakAsync("yes sir list commands will hide "); }
                    if (renNum == 2) { hana.SpeakAsync("alright sir ");}
                    listcommands.Visible = false;
                }

        }
        private void _reconizer_SpeechReccpgnized(object sender, SpeechDetectedEventArgs e)
        {
            timeout = 0;

        }

        private void start_listening_SpeechReccpgnized(object sender, SpeechRecognizedEventArgs e)
        {
            string speech = e.Result.Text;
            if (speech == "wake up " || speech == "hana") {
                start_listening.RecognizeAsyncCancel();
                hana.SpeakAsync("yes  i am online do u have any command");
                _reconizer.RecognizeAsync(RecognizeMode.Multiple);
            }

        }

        private void timspeek_Tick(object sender, EventArgs e)
        {
            if( timeout ==10){
                _reconizer.RecognizeAsyncCancel();

            }
            else if (timeout == 11)
            {
                timspeek.Stop();
                start_listening.RecognizeAsync(RecognizeMode.Multiple);
                timeout = 0;

            }
        }

        private void listcommands_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
...