SetInputToWaveFile
i am new to c# and am currently working on a project that involves me using an audio file as input and write it as text. I have the code which uses the system.speech. I got this code from http://blog.thomascsherman.com/2009/08/getting-started-with-windows-voice-recognition/ and it compiles, but i am having trouble at run time. As soon as i run it the code crashes and the console says "the input file is not found". I realised what the path for the input file is and i know the the fil开发者_Go百科e is in that folder. Please help me with this. I am pasting the code below.
using System; using System.IO; using System.Speech; using System.Speech.Recognition; using System.Speech.AudioFormat;
public class Hello {
public static void Main()
{
Hello hello= new Hello();
hello.doStuff();
}
private void doStuff()
{
SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
sre.SpeechHypothesized += new EventHandler<SpeechHypothesizedEventArgs>(sre_SpeechHypothesized);
sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
sre.RecognizeCompleted += new EventHandler<RecognizeCompletedEventArgs>(sre_RecognizeCompleted);
sre.AudioSignalProblemOccurred += new EventHandler<AudioSignalProblemOccurredEventArgs>(sre_AudioSignalProblemOccurred);
sre.SpeechDetected += new EventHandler<SpeechDetectedEventArgs>(sre_SpeechDetected);
sre.SpeechRecognitionRejected += new EventHandler<SpeechRecognitionRejectedEventArgs>(sre_SpeechRecognitionRejected);
DictationGrammar dg = new DictationGrammar();
sre.LoadGrammar(dg);
sre.SetInputToWaveFile("hello.wav");
sre.EndSilenceTimeout = new TimeSpan(0, 0, 2);
sre.RecognizeAsync();
}
void sre_SpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
{
Console.WriteLine(e.Result.Text);
}
void sre_AudioSignalProblemOccurred(object sender, AudioSignalProblemOccurredEventArgs e)
{
Console.WriteLine(e.AudioSignalProblem.ToString());
}
void sre_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
{
Console.WriteLine("Rejected!");
}
void sre_RecognizeCompleted(object sender, RecognizeCompletedEventArgs e)
{
Console.WriteLine("Recognition Complete!");
}
void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine(e.Result.Text);
}
void sre_SpeechDetected(object sender, SpeechDetectedEventArgs e)
{
Console.WriteLine("Speech Detected!");
}
}
To begin with, try setting an absolute path (I.e. @"C:/my/file/is/here/hello.wav") to determine if it's the path that's the problem or something else.
Also note that there are different folders for Release and Debug builds, which can be confusing at times. Make sure that the file really is in the correct folder (to test if this is the problem, put hello.wav in both Release and Debug folders to begin with).
精彩评论