【发布时间】:2010-05-21 05:38:13
【问题描述】:
我的 C# 项目中有这段代码:
public void startRecognition(string pName)
{
presentationName = pName;
if (WaveNative.waveInGetNumDevs() > 0)
{
string grammar = System.Environment.GetEnvironmentVariable("PUBLIC") + "\\SoundLog\\Presentations\\" + presentationName + "\\SpeechRecognition\\soundlog.cfg";
if (File.Exists(grammar))
{
File.Delete(grammar);
}
executeCommand();
/// Create an instance of SpSharedRecoContextClass which will be used
/// to interface with the incoming audio stream
recContext = new SpSharedRecoContextClass();
// Create the grammar object
recContext.CreateGrammar(1, out recGrammar);
//recContext.CreateGrammar(2, out recGrammar2);
// Set up dictation mode
//recGrammar2.SetDictationState(SpeechLib.SPRULESTATE.SPRS_ACTIVE);
//recGrammar2.SetGrammarState(SPGRAMMARSTATE.SPGS_ENABLED);
// Set appropriate grammar mode
if (File.Exists(grammar))
{
recGrammar.LoadCmdFromFile(grammar, SPLOADOPTIONS.SPLO_STATIC);
//recGrammar.SetDictationState(SpeechLib.SPRULESTATE.SPRS_INACTIVE);
recGrammar.SetGrammarState(SPGRAMMARSTATE.SPGS_ENABLED);
recGrammar.SetRuleIdState(0, SPRULESTATE.SPRS_ACTIVE);
}
/// Bind a callback to the recognition event which will be invoked
/// When a dictated phrase has been recognised.
recContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(handleRecognition);
// System.Windows.Forms.MessageBox.Show(recContext.ToString());
// gramática compilada
}
}
private static void handleRecognition(int StreamNumber,
object StreamPosition,
SpeechLib.SpeechRecognitionType RecognitionType,
SpeechLib.ISpeechRecoResult Result)
{
string temp = Result.PhraseInfo.GetText(0, -1, true);
_recognizedText = "";
// System.Windows.Forms.MessageBox.Show(temp);
// System.Windows.Forms.MessageBox.Show(recognizedWords.Count.ToString());
foreach (string word in recognizedWords)
{
if (temp.Contains(word))
{
// System.Windows.Forms.MessageBox.Show("yes");
_recognizedText = word;
}
}
}
这段代码会生成一个我在另一个应用程序中使用的 dll。
现在,邪恶的虫子: - 当我在其他应用程序的执行开始时运行 startRecognition 方法时,此代码运行良好。但是当我在开始后一段时间运行它时,这段代码有效,但从未调用过 handleRecognition 方法。我看到这些词已被识别,因为它们出现在 Microsoft Speech Recognition 应用程序中,但从未调用处理程序方法。
你知道这段代码有什么问题吗?
注意:这个项目有一些一直在执行的代码。这可能是问题吗?因为其他代码正在运行它不允许它运行?
【问题讨论】:
-
这可能是所有的 cmets 都让编译器生气并对你耍小把戏:P
-
只是想知道您是否尝试过将处理程序设为虚拟而不是静态?
-
我稍后会尝试虚拟的东西:)
-
注意:这个项目有一些一直在执行的代码。这可能是问题吗?因为其他代码正在运行它不允许它运行?
标签: c# .net speech-recognition