C# Visual Studio 2012 语音到 textBox1

Posted

技术标签:

【中文标题】C# Visual Studio 2012 语音到 textBox1【英文标题】:C# Visual Studio 2012 Speech to textBox1 【发布时间】:2014-05-29 19:21:05 【问题描述】:

我遇到了语音 API 的问题。我正在使用命令,这很好,但是当我希望在 textBox1 中看到语音时,它不会显示它。

这是我需要帮助的代码。我正在使用开关盒。我尝试了几个 if 语句,但没有一个有效。

case "listen":
                    AI.Speak("I am listening");
                    textBox1.Text = textBox1.Text + " " + e.Result.Text.ToString();
                    break;

每次我说听时,文本框中只有“听”可见

这里是完整的代码:

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.Threading;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.IO;
using System.Xml;
using System.Web;
using WindowsMicrophoneMuteLibrary;
using TweetSharp;
/*                                          
 *                                          
 *                                         
 *  
 *                                          
 * 
 */

namespace Test

    public partial class Form1 : Form
    
        SpeechRecognitionEngine sRecognizer = new SpeechRecognitionEngine();
        SpeechSynthesizer AI = new SpeechSynthesizer();
        DateTime now = DateTime.Now;
        Random rnd = new Random();
        WindowsMicMute micMute = new WindowsMicMute();
        TwitterService twitter = new TwitterService("--", "--", "--", "--");
        //string QEvent;
        //string ProcWindow;
        //double timer = 10;
        //int count = 1;

        public Form1()
        
            InitializeComponent();
        

        private void Form1_Load(object sender, EventArgs e)
        
            sRecognizer.SetInputToDefaultAudioDevice();
            sRecognizer.LoadGrammar(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"D:\Bibliotheek\Mijn Documenten\Commands.txt")))));
            sRecognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(rSpeechRecognized);
            sRecognizer.RecognizeAsync(RecognizeMode.Multiple);
            // LAAD COMMANDS BIJ START-UP
            string[] commands = (File.ReadAllLines(@"D:\Bibliotheek\Mijn Documenten\Commands.txt"));
            lstCommands.Items.Clear();
            lstCommands.SelectionMode = SelectionMode.None;
            foreach (string command in commands)
            
                lstCommands.Items.Add(command);
            
        

        void rSpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        
            int ranNum = rnd.Next(1, 10);
            string speech = e.Result.Text;
            switch (speech)
            
                // GROETEN
                case "hello":                               // ALS "hello" WORDT INGESPROKEN IN DE MICROFOON
                    if (ranNum <= 3)                         
                    
                        AI.Speak("Hello Sir");          // ALS RANDOM NUMMER < 5 IS = "hello sir"
                    
                    else if (ranNum >= 4 && ranNum <= 6)                    
                    
                        AI.Speak("Greetings");          // ALS RANDOM NUMMER >= 5 IS = "greetings"
                    
                    else if (ranNum >= 7)
                    
                        AI.Speak("Good day to you");
                    
                    break;
                case "AI":                              // ALS "AI" WORDT INGESPROKEN IN DE MICROFOON
                    if (ranNum <= 4)                         
                    
                        AI.Speak("Yes Sir");            // ALS RANDOM NUMMER < 5 IS = "yes sir"
                    
                    else if (ranNum >= 5)                   
                    
                        AI.Speak("Yes?");               // ALS RANDOM NUMMER >= 5 IS = "yes?"
                    
                    break;

                // SLUIT
                case "exit program":                        // ALS "exit program" WORDT INGESPROKEN IN DE MICROFOON
                    AI.Speak("Until next time");        
                    this.Close();                                // APPLICATIE WORDT GESLOTEN
                    break;

                // WEBSITES
                case "open google":                         // ALS "open google" WORDT INGESPROKEN IN DE MICROFOON
                    System.Diagnostics.Process.Start("http://www.google.nl");   // GOOGLE WORDT GEOPEND
                    break;
                case "open youtube":                        // ALS "open youtube" WORDT INGESPROKEN IN DE MICROFOON
                    System.Diagnostics.Process.Start("https://www.youtube.com/feed/subscriptions"); // YOUTUBE WORDT GEOPEND
                    break;
                case "open tweakers":                        // ALS "tweakers" WORDT INGESPROKEN IN DE MICROFOON
                    System.Diagnostics.Process.Start("http://tweakers.net/"); // TWEAKERS WORDT GEOPEND
                    break;

                // PROGRAMMA'S
                case "run guild wars":                      // ALS "run guild wars" WORDT INGESPROKEN IN DE MICROFOON
                    System.Diagnostics.Process.Start("D:\\Entertainment\\Guild Wars 2\\Gw2.exe");   // GUILD WARS 2 WORDT GEOPEND
                    AI.Speak("Loading program");
                    break;

                // GEGEVENS VAN DE DAG
                case "whats the time":                     // ALS "what time is it" WORDT INGESPROKEN IN DE MICROFOON
                    AI.Speak(DateTime.Now.ToString("HH:mm"));           // TIJD VAN DE DAG WORDT VERTELD
                    break;
                case "whats the day":                      // ALS "what day is it" WORDT INGESPROKEN IN DE MICROFOON
                    AI.Speak(DateTime.Today.ToString("dddd"));          // DAG VAN VANDAAG WORDT VERTELD
                    break;
                case "whats the date":                      // ALS "whats the date" WORDT INGESPROKEN IN DE MICROFOON
                    AI.Speak(DateTime.Today.ToString("dd-MMM-yyyy"));   // DATUM VAN VANDAAG WORDT VERTELD
                    break;

                // ANDERE COMMANDS
                case "go fullscreen":                       // ALS "go fullscreen" WORDT INGESPROKEN IN DE MICROFOON
                    FormBorderStyle = FormBorderStyle.None;
                    WindowState = FormWindowState.Maximized;
                    TopMost = true;
                    AI.Speak("Going into fullscreen mode");
                    break;
                case "exit fullscreen":                     // ALS "exit fullscreen" WORDT INGESPROKEN IN DE MICROFOON
                    FormBorderStyle = FormBorderStyle.Sizable;
                    WindowState = FormWindowState.Normal;
                    TopMost = false;
                    AI.Speak("Exiting fullscreen mode");
                    break;

                // TWITTER
                case "post on twitter":
                    if (listBox1.Visible == true)
                    
                        this.textBox1.Location = new System.Drawing.Point(89, 163);
                        this.label1.Location = new System.Drawing.Point(18, 166);
                    
                    textBox1.Visible = true;
                    label1.Visible = true;
                    break;
                case "post":
                    if (textBox1.Visible == false)
                    
                        AI.Speak("say post on twitter first");
                    
                    else if (String.IsNullOrEmpty(textBox1.Text.Trim()))
                    
                        AI.Speak("you will have to write down something");
                    
                    else
                    
                        twitter.SendTweet(new SendTweetOptions()  Status = textBox1.Text );
                        AI.Speak("Your tweet has been posted");
                        textBox1.Clear();
                    
                    break;
                case "clear post":
                    textBox1.Visible = false;
                    label1.Visible = false;
                    break;
                case "show tweets":
                    listBox1.Visible = true;
                    label2.Visible = true;
                    if (textBox1.Visible == true)
                    
                        this.textBox1.Location = new System.Drawing.Point(89, 163);
                        this.label1.Location = new System.Drawing.Point(18, 166);
                    
                    listBox1.Visible = true;
                    label2.Visible = true;
                    listBox1.Items.Clear();
                    var getTweets = twitter.ListTweetsOnHomeTimeline(new ListTweetsOnHomeTimelineOptions()  Count = 10 );
                    foreach (var tweets in getTweets)
                    
                        listBox1.Items.Add(tweets.Text);
                    
                    break;
                case "clear tweets":
                    listBox1.Visible = false;
                    label2.Visible = false;
                    this.textBox1.Location = new System.Drawing.Point(89, 9);
                    this.label1.Location = new System.Drawing.Point(18, 12);
                    break;
                case "update tweets":
                    if (listBox1.Visible == false)
                    
                        AI.Speak("I cant update without getting the tweets first");
                    
                    else
                    
                        listBox1.Items.Clear();
                        var update = twitter.ListTweetsOnHomeTimeline(new ListTweetsOnHomeTimelineOptions()  Count = 10 );
                        foreach (var tweets in update)
                        
                            listBox1.Items.Add(tweets.Text);
                        
                    
                    break;

                **case "listen":
                    AI.Speak("I am listening");
                    textBox1.Text = textBox1.Text + " " + e.Result.Text.ToString();
                    break;**


                /*
                case "show commands":
                    string[] commands = (File.ReadAllLines(@"D:\Bibliotheek\Mijn Documenten\Commands.txt"));
                    JARVIS.Speak("Very well");
                    lstCommands.Items.Clear();
                    lstCommands.SelectionMode = SelectionMode.None;
                    lstCommands.Visible = true;
                    foreach (string command in commands)
                    
                        lstCommands.Items.Add(command);
                    
                    break;
                case "hide commands":
                    lstCommands.Visible = false;
                    break;
                 */
            
        

        private void lstCommands_SelectedIndexChanged(object sender, EventArgs e)
        

        

        private void btnMic_Click(object sender, EventArgs e)
        
            if (btnMic.Text == "Mute")
            
                btnMic.Text = "Unmute";
                micMute.MuteMic();
                AI.Speak("Muted");
            
            else if (btnMic.Text == "Unmute")
            
                btnMic.Text = "Mute";
                micMute.UnMuteMic();
                AI.Speak("Unmuted");
            
        
    

编辑:

我需要有关这段代码的帮助:

case "listen":
                    AI.Speak("I am listening");
                    textBox1.Text = textBox1.Text + " " + e.Result.Text.ToString();
                    break;

当我说“听”时,AI 会跟着“我在听”。之后,它应该将文本即时消息放入 textbox1 中的麦克风中,但它没有。它只在其中放置“听”。

其他一切正常!

【问题讨论】:

那是很多代码。请将您的问题编辑成一个简洁的问题示例。 e.Result.Text.ToString() 不是已经设置好了,你需要调用一些东西来捕捉语音吗? 【参考方案1】:
case "listen":
    AI.Speak("I am listening");
    textBox1.Text = textBox1.Text + " " + e.Result.Text.ToString();
    break;

e.Result 将仅包含用户说“听”时获得的结果。 在你让 AI 说“我在听”之后,你实际上需要再次倾听用户。

case "listen":
    AI.Speak("I am listening");
    var result = sRecognizer.Recognize();
    textBox1.Text += " " + result.Text;
    break;

【讨论】:

我现在收到此错误:在识别器进行识别时无法执行此操作。所以当我说听的时候识别器已经激活了,这是否意味着我不需要再次调用它,对吧。 那是因为你还没有处理完当前的语音("Listen")。你将不得不改变程序的流程来解决这个问题。您可以保存最后一个语音命令,如果最后一个语音命令是“听”,则继续将当前结果写入文本框。或者根据需要中断当前流程并使用无限循环来托管主要流程和分支。【参考方案2】:

你说“听”的速度有多快? 我怀疑你的问题是语音 API 只是在寻找第一个“Listen”,然后在你再次说“Listen”时它正忙于解析数据。我相信另一个答案正确地解决了这个问题 - 一旦你认识到语音文本,您必须再次调用识别。

试着放慢语速,或者说听(等待命令解析)然后再听。它是否按预期工作?

您可能想调查using asych methods

[编辑] 更改描述后,试试这个:

textBox1.Text = textBox1.Text + " " + speech;

【讨论】:

不管我做什么,我都会得到: 识别器进行识别时无法执行此操作;当我在那里放一个 var 时。

以上是关于C# Visual Studio 2012 语音到 textBox1的主要内容,如果未能解决你的问题,请参考以下文章

在 Visual Studio 2012 中发布 C# winform 应用程序

C# 关键字 Visual Studio 2012

MS Visual Studio 2012 Express 是不是同时包含 C#、Visual Basic、C++? [关闭]

Visual Studio 2012(C#)中的可折叠(条件、循环)块[重复]

在 Microsoft Visual Studio 2012 中使用 Java

用于区分 Visual Studio 2012 和 2010 的 C# 预处理器指令或条件?