2016-04-06 11 views
-2

こんにちは、私はm new in this C# so i don私は音声コマンドを使用してGoogleで検索することができます^^あなたが私に教えてくれると私は感謝します。ここで C#声でGoogle検索

は、ステップによって私の全体code.Explainステップでください。D

{

public partial class Form1 : Form 
{   

    SpeechSynthesizer s = new SpeechSynthesizer(); 
    Choices list = new Choices(); 
    Boolean wake = true; 
    public Form1() 
    { 
     SpeechRecognitionEngine rec = new SpeechRecognitionEngine(); 
     list.Add(new String[] { "hello", "how are you", "what time is it", "what day is it", "open google", "wake", "sleep", "restart", "open studio", "close studio", "search", "standby", "mute", "unmute", "up", "down", "hex system up"}); 
     Grammar gr = new Grammar(new GrammarBuilder(list)); 
     try 
     { 
      rec.RequestRecognizerUpdate(); 
      rec.LoadGrammar(gr); 
      rec.SpeechRecognized += rec_SpeachRecognized; 
      rec.SetInputToDefaultAudioDevice(); 
      rec.RecognizeAsync(RecognizeMode.Multiple); 
     } 
     catch { return; } 


     InitializeComponent(); 
    } 
    public void KillProg(String s) 
    { 

     System.Diagnostics.Process[] procs = null; 
     try 
     { 
      procs = Process.GetProcessesByName(s); 
      Process prog = procs[0]; 
      if (!prog.HasExited) { prog.Kill(); } 


    }finally 
     { 
      if (procs != null) 
      { 
       foreach (Process p in procs) 
       { 
        p.Dispose(); 
       } 
      } 
     } 
}  


    public void restart() 
    { 
     Process.Start(@"D:\here.exe"); 
     Environment.Exit(0); 
    } 
    public void say(String h) 
    { 

     s.Speak(h); 

    } 

    public static void ExecuteCommand(string Command) 
    { 
     System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c" + Command); 
     procStartInfo.RedirectStandardOutput = true; 
     procStartInfo.UseShellExecute = false; 
     procStartInfo.CreateNoWindow = true; 
     System.Diagnostics.Process proc = new System.Diagnostics.Process(); 
     proc.StartInfo = procStartInfo; 
     proc.Start(); 
     proc.Close(); 
    } 
    //Speech Commands 
    private void rec_SpeachRecognized(object sender, SpeechRecognizedEventArgs e) 
    {    
     String r = e.Result.Text; 
     if (r == "wake") { say("The system is listening sir"); } wake = true; 
     if (r == "sleep") { say("The system will no more listen sir"); } wake = false; 
     if (wake == true)    
     { 
      if (r == "hex system up") {Process.Start(@"C:\Program Files\Rainmeter\Rainmeter.exe"); say("Welcome back sir     the system is loading and all energy is stabilized  ,   now i am at 100% capacity"); } 
      if (r == "down") { ExecuteCommand("C:/nircmd.exe changesysvolume -10000"); } 
      if (r =="up") {ExecuteCommand ("C:/nircmd.exe changesysvolume 10000");} 
      if (r == "unmute"){ExecuteCommand ("C:/nircmd.exe mutesysvolume 0"); say("system unmute sir");} 
      if (r == "mute") {say("mute now!"); ExecuteCommand ("C:/nircmd.exe mutesysvolume 1");} 
      if (r == "standby") { say("The system will enter in waiting mode sir"); ExecuteCommand("C:/nircmd.exe standby"); }  
      if (r == "close studio") { KillProg("WDExpress"); } 
      if (r == "open studio") { Process.Start(@"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\WDExpress.exe"); } 
      if (r == "hello") { say("Hi"); } 
      if (r == "how are you") { say("Great , and you?"); } 
      if (r == "what time is it") { say(DateTime.Now.ToString("hh:mm")); } 
      if (r == "what day is it") { say(DateTime.Now.ToString("M/d/yyyy")); } 
      if (r == "open google") { Process.Start("https://www.google.ro"); } 
     } 





    } 

    private void say() 
    { 
     throw new NotImplementedException(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 

    public bool c { get; set; } 

    public int r { get; set; } 

    public int rnd { get; set; } 
} 

:D私は新しいライブラリまたは新規の入り口か、新しい「使用」のものを追加する必要がある場合(教えてください}

答えて

1

SpeechRecognitionEngineには、複数の「モード」があります。コマンドモードで使用しているため、認識できるように事前に指定された単語を使用できます。任意の単語を認識できるディクテーションモードで使用するには、文法としてのSystem.Speech.Recognition.DictationGrammarのあなたのSpeechRecognitionEngineをそうすることによって:rec.LoadGrammar(new DictationGrammar());。これが完了すると、SpeechRecognitionEngineは任意の単語を認識します。


は、実際のGoogle検索の一部を行うには、そうすることによって、Googleで何かを検索することができます。 System.Diagnostics.Process.Start("http://www.google.com/search?q=" + StringToSearchFor)。これは rec_SpeechRecognizedメソッドで使用できます。たとえば、認識されたテキストが「google」で始まっているかどうかを確認し、それに続くものを検索することができます。

例:

private void rec_SpeachRecognized(object sender, SpeechRecognizedEventArgs e) 
    { 
     if (e.Result.Text.StartsWith("google ")) 
     { 
      System.Diagnostics.Process.Start(
       "https://www.google.com/search?q=" + e.Result.Text.Substring(7) //"google " is 7 characters long. 
      ); 
     } 
     ... 
    } 

関連ブログ記事:http://csharp-tricks-en.blogspot.dk/2011/03/speech-recognition-part-1-dictation-mode.html

+0

ちょっと答えたがために男のおかげ:ジのために、Googleで単語を検索するために、ランダムな単語、その後、プログラムを言いたいです例:私が犬を言うと、プログラムはgoogleで犬や猫を検索しますが、私は何も言いません:Dあなたは音声コマンドがランダムな単語であることを知っています –