1
Answer

How do I handel events like speech or key press in C# Servic

Afraz Afaq

Afraz Afaq

7y
289
1
I am trying to make a program basically a voice support so that recognizes speech and does the corresponding work I want this program to work even after the pc is in sleep state thats why i want service but after installing service successfully it does not hear any voice.
  1. public partial class Service1 : ServiceBase  
  2.     {  
  3.         SpeechSynthesizer s = new SpeechSynthesizer();  
  4.         SpeechRecognitionEngine rec = new SpeechRecognitionEngine();  
  5.         Choices list = new Choices();  
  6.         
  7.         public Service1()  
  8.         {  
  9.             s.SelectVoiceByHints(VoiceGender.Male)  
  10.             s.Speak("heyy");  
  11.             list.Add(new String[] { "hello""how are you" });  
  12.             Grammar gr = new Grammar(new GrammarBuilder(list));  
  13.             try  
  14.             {  
  15.                 rec.RequestRecognizerUpdate();  
  16.                 rec.LoadGrammar(gr);  
  17.                 rec.SpeechRecognized += rec_speachrecocnized;  
  18.                 rec.SetInputToDefaultAudioDevice();  
  19.                 rec.RecognizeAsync(RecognizeMode.Multiple);  
  20.   
  21.   
  22.             }  
  23.             catch { return; };  
  24.              
  25.             InitializeComponent();  
  26.         }  
  27.   
  28.         public void speak(String h)  
  29.         {  
  30.   
  31.             s.Speak(h);  
  32.         }  
  33.   
  34.         private void rec_speachrecocnized(object sender, SpeechRecognizedEventArgs e)  
  35.         {  
  36.                 String r = e.Result.Text;  
  37.                 if (r == "hello")  
  38.                 {  
  39.   
  40.                     speak("hello");  
  41.                 }  
  42.   
  43.                 if (r == "how are you")  
  44.                 {  
  45.   
  46.                     speak("fine");  
  47.                 }  
  48.         }  
  49.          
  50.           
  51.         protected override void OnStart(string[] args)  
  52.         {  
  53.   
  54.         }  
  55.   
  56.         protected override void OnStop()  
  57.         {  
  58.         }  
  59.     }  

Attachment: Service1.rar

Answers (1)