2017-08-28 62 views
0

我有一個小型的聲音指揮程序,我正在努力,並且我想讓它在給定某些命令時要求確認......就像「嘿計算機,關閉程序」,然後是口頭問題「你確定嗎?」。之後迴應我的口頭答覆;是或否。我對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; 
    } 
} 

回答

0

您可以執行命令(你說)前,運行一個事件,並請求許可。但請注意,此解決方案適用於用戶界面。對於antoher解決方案看選項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 
      } 

這是一個是一個解決方案,其中反饋可以不與列表,是一種歷史的工作synchonously發生。

當你想回答「你確定嗎?」時,你可以使用一個列表。這意味着,您啓動一​​個命令,並將關於此命令的信息放入列表中,並立即運行一個讀取列表條目的函數。現在你應該使用關於這些命令的信息,刪除無效的條目,當它們爲真時執行功能等等。

這裏一個小例子:

選項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 
     } 
相關問題