设置输入到波形文件
我是 C# 新手,目前正在开发一个项目,该项目涉及我使用音频文件作为输入并将其写入文本。我有使用 system.speech 的代码。我从 http://blog.thomascsherman 获得此代码.com/2009/08/getting-started-with-windows-voice-recognition/ 它可以编译,但我在运行时遇到问题。一旦我运行它,代码就会崩溃,控制台显示“找不到输入文件”。我意识到输入文件的路径是什么,并且我知道该文件位于该文件夹中。请帮我解决这个问题。我粘贴下面的代码。
使用系统; 使用系统.IO; 使用系统语音; 使用系统.语音.识别; 使用 System.Speech.AudioFormat;
公开课你好 {
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!");
}
}
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 file 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!");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,尝试设置绝对路径(即@“C:/my/file/is/here/hello.wav”)以确定是路径有问题还是其他原因。
另请注意,发布和调试版本有不同的文件夹,有时可能会造成混淆。确保文件确实位于正确的文件夹中(要测试这是否是问题所在,首先将 hello.wav 放入 Release 和 Debug 文件夹中)。
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).