C# 使用语音命令时,如何使程序请求确认?

C# 使用语音命令时,如何使程序请求确认?,c#,command,voice,speech,confirmation,C#,Command,Voice,Speech,Confirmation,我有一个小的语音指挥程序,我正在工作,我想让它要求确认时,给予某些命令。。。比如“嘿,电脑,关闭程序”,然后是口头问题“你确定吗?”然后回答我的口头回答;是或否。我对c#相当陌生,找不到任何相关信息。以下代码是我已配置的语音命令示例: private void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { switch (e.Result.Text) {

我有一个小的语音指挥程序,我正在工作,我想让它要求确认时,给予某些命令。。。比如“嘿,电脑,关闭程序”,然后是口头问题“你确定吗?”然后回答我的口头回答;是或否。我对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;
    }
}

您可以在执行命令(讲话)之前运行事件并请求权限。但请注意,此解决方案使用的是用户界面。有关解决方案,请参见选项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
            }
         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
        }
publicsomeclass(){
PermissionEvent+=此PermissionEvent;
}
private void此\u PermissionEvent(对象发送方、PermissionEventArgs args args){
//MessageBox.Show(…)将等待您关闭窗口(是/否/关闭/终止)
if(MessageBox.Show(“您想执行吗?”,“授予权限”,MessageBoxButtons.YesNo)=DialogResult.Yes){
args.Handle=true;
}
}
私有事件处理程序PermissionEvent;
私有void recEngine_SpeechRecognized(对象发送方,SpeechRecognizedEventArgs e)
{
开关(如Result.Text)
{                
案例“嘿,电脑,启动spotify”:
//
var args=新的PermissionEventArgs();
//args是引用类型。这意味着,当您更改此_PermissionEvent(…)中的args.Handle时,此args.Handle也会更改。
PermissionEvent?.Invoke(args)
//这里我们检查可能发生变化的条件。
if(args.Handle){
//在这里做点什么
}
打破
}
}
公共类PermissionEventArgs:EventArgs{
公共布尔句柄=false
}
这是一种解决方案,在这种解决方案中,反馈可以同步发生,而无需使用列表,一种历史记录

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

这里有一个小例子:

选项2:

    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
            }
         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
        }
列表列表;//此列表包含命令
公共类(){
list=new list();//在构造函数中初始化列表
}
私有无效验证列表(){
如果(list.Count==0)
返回;
if(list[0].Cmd==Command.Yes | | list[0].Cmd==Command.No){
list.Clear();
}否则如果(!list[0].NeedConfirm | |(list.Count==2&&list[1].Cmd==Command.Yes)){
列表[0]。调用?.Invoke();
list.Clear();
}否则如果(list.Count>=2){
list.Clear();
}
}
私有void recEngine_SpeechRecognized(对象发送方,SpeechRecognizedEventArgs e)
{
开关(如Result.Text)
{                
案例“嘿,电脑,启动spotify”:
list.Add(newentry(){Cmd=Command.Yes,Call=newaction(()=>{
//开始spotify
}) });
验证列表();
打破
案例“确认命令”:
添加(新条目(){Cmd=Command.Yes});
验证列表();
打破
案例“不确认命令”:
添加(新条目(){Cmd=Command.No});
验证列表();
打破
}
}
//这个类包含一个命令的所有重要内容。
// 1. 命令的种类
// 2. 如果verifyList()中的条件为true,则执行的操作
// 3. 并且需要确认VerificationList()需要
公开课入学{
命令命令Cmd;
行动呼吁;
布尔需要确认;
}
公共枚举命令{
StartSpotify,
对
不
}