【问题标题】:How do I make my program ask for confirmation when using a speech command?使用语音命令时如何让我的程序要求确认?
【发布时间】:2017-08-28 09:02:36
【问题描述】:

我正在开发一个小型语音命令程序,我想让它在收到特定命令时要求确认...例如“嘿,计算机,关闭程序”,然后是口头问题“你确定吗? "然后回复我的口头回答;是还是不是。我对 c# 还很陌生,找不到任何相关的东西。以下代码是我已经配置的语音命令示例:

private void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
    switch (e.Result.Text)
    {                
        case "hey computer, start spotify":
            synthesizer.SelectVoiceByHints(VoiceGender.Female);
            synthesizer.SpeakAsync("starting SPOTteFY");
            string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string extentionToPath = "Spotify\\Spotify.exe";
            string finalPath = Path.Combine(appDataPath, extentionToPath);

            Process.Start(finalPath);
            //Process.Start("C:\\Users\\Danny\\AppData\\Roaming\\Spotify\\Spotify.exe");
            break;
        case "hey computer, start chrome":
            synthesizer.SelectVoiceByHints(VoiceGender.Female);
            synthesizer.SpeakAsync("Starting Chrome");
            Process.Start("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
            break;
        case "hey computer, new tab":
            SendKeys.Send("^t");
            break;
        case "hey computer, close program":
            synthesizer.SelectVoiceByHints(VoiceGender.Female);
            synthesizer.SpeakAsync("Closing program");
            SendKeys.Send("%{F4}");
            break;
        case "next song please":
            keybd_event(VK_MEDIA_NEXT_TRACK, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
            break;
        case "stop song please":
            keybd_event(VK_MEDIA_PLAY_PAUSE, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero);
            break;
        case "hey computer, start netflix":
            synthesizer.SelectVoiceByHints(VoiceGender.Female);
            synthesizer.SpeakAsync("Starting Netflix");
            System.Diagnostics.Process.Start("https://www.netflix.com/browse");
            break;
        case "hey computer, pause netflix":
            SendKeys.Send(" ");
            break;
        case "hey computer, start reddit":
            synthesizer.SelectVoiceByHints(VoiceGender.Female);
            synthesizer.SpeakAsync("Starting reddit");
            System.Diagnostics.Process.Start("https://www.reddit.com");
            break;
        case "hey computer, show me the news":
            synthesizer.SelectVoiceByHints(VoiceGender.Female);
            synthesizer.SpeakAsync("Showing you what's going on");
            System.Diagnostics.Process.Start("http://nu.nl");
            break;

        case "hey computer, hide yourself":
            this.WindowState = FormWindowState.Minimized;
            break;
    }
}

【问题讨论】:

    标签: c# command voice speech confirmation


    【解决方案1】:

    您可以在执行命令(您说话)之前运行一个事件并请求许可。但请注意,此解决方案适用于用户界面。对于另一种解决方案,请查看选项 2。

    选项 1:

        public SomeClass() {
                PermissionEvent += this_PermissionEvent;
                }
    
                private void this_PermissionEvent(object sender, PermissionEventArgs args) {
            // MessageBox.Show(...) waits until you closed the window (yes/no/closed/terminated)
                if (MessageBox.Show("Do you want execute?","Grant Permission",MessageBoxButtons.YesNo) == DialogResult.Yes) {
            args.Handle = true;
            }
                }
    
                private event EventHandler<PermissionEventArgs> PermissionEvent;
    
                private void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
                {
                switch (e.Result.Text)
                {                
                case "hey computer, start spotify":
                //
                var args = new PermissionEventArgs();
        // args is a reference type. That means, when you change args.Handle in this_PermissionEvent(...) then this args.Handle is also changed.
                PermissionEvent?.Invoke(args)
    
        // here we check the condition that might be changed.
                if (args.Handle) {
                // do something here
                }
                break;
                }
                }
    
                public class PermissionEventArgs : EventArgs {
                public bool Handle = false
                }
    

    这是一种解决方案,可以在不使用列表(一种历史记录)的情况下同步发生反馈。

    当您希望以这种方式回答“您确定吗?”时,您可以使用列表。这意味着,您启动一​​个命令并将有关此命令的信息放入一个列表中,然后立即运行一个读取列表条目的函数。现在您应该使用有关这些命令的信息,删除无效条目,在它们为真时执行函数等等。

    这里是一个小例子:

    选项 2:

             List<Entry> list; // This list contains the commands
    
                public SomeClass() {
                list = new List<Entry>(); // initializion of the list in the constructor
                }
    
                private void verifyList() {
                if (list.Count == 0)
                return;
    
                if (list[0].Cmd == Command.Yes || list[0].Cmd == Command.No) {
                list.Clear();
                } else if (!list[0].NeedConfirm || (list.Count == 2 && list[1].Cmd == Command.Yes)) {
                list[0].Call?.Invoke();
                list.Clear();
                } else if (list.Count >= 2) {
                list.Clear();
                }
                }
    
                private void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
                {
                    switch (e.Result.Text)
                    {                
                        case "hey computer, start spotify":
                list.Add(new Entry() { Cmd = Command.Yes, Call = new Action(() => {
                // start spotify
                }) });
                verifyList();
                            break;
                        case "confirm command": 
                list.Add(new Entry() { Cmd = Command.Yes });
                verifyList();
                break;
     case "do not confirm command": 
                list.Add(new Entry() { Cmd = Command.No });
                verifyList();
                break;
                }
                }
    
        // This class holds all important stuff of one command. 
        // 1. The kind of command
        // 2. the action that performs if condition in verifyList() are true
        // 3. And NeedConfirm that the verifiyList() needs
            public class Entry {
            Command Cmd;
            Action Call;
            bool NeedConfirm;
            }
    
            public enum Command {
            StartSpotify,
            Yes,
            No
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多