Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
1
Reply
How do I handel events like speech or key press in C# Servic
Afraz Afaq
Sep 26 2017 2:34 AM
243
Reply
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.
public
partial
class
Service1 : ServiceBase
{
SpeechSynthesizer s =
new
SpeechSynthesizer();
SpeechRecognitionEngine rec =
new
SpeechRecognitionEngine();
Choices list =
new
Choices();
public
Service1()
{
s.SelectVoiceByHints(VoiceGender.Male)
s.Speak(
"heyy"
);
list.Add(
new
String[] {
"hello"
,
"how are you"
});
Grammar gr =
new
Grammar(
new
GrammarBuilder(list));
try
{
rec.RequestRecognizerUpdate();
rec.LoadGrammar(gr);
rec.SpeechRecognized += rec_speachrecocnized;
rec.SetInputToDefaultAudioDevice();
rec.RecognizeAsync(RecognizeMode.Multiple);
}
catch
{
return
; };
InitializeComponent();
}
public
void
speak(String h)
{
s.Speak(h);
}
private
void
rec_speachrecocnized(
object
sender, SpeechRecognizedEventArgs e)
{
String r = e.Result.Text;
if
(r ==
"hello"
)
{
speak(
"hello"
);
}
if
(r ==
"how are you"
)
{
speak(
"fine"
);
}
}
protected
override
void
OnStart(
string
[] args)
{
}
protected
override
void
OnStop()
{
}
}
Attachment:
Service1.rar
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
1
)
Next Recommended Forum
I am Trying To Build An Empty Project In Visual 2017?
Submitting Universal Windows App To Store ?