我想寫一個C#應用程序使用Windows窗體和System.Speech將WAV文件轉換爲文本。我在網上看到很多關於如何做到這一點的示例,但沒有一個是非常強大的。我希望編寫一個應用程序,它可以使用BackgroundWorker線程解析較小的大型WAV文件,但在我的線程的DoWork函數中,當它調用engine.Recognize()時會收到以下異常:SpeechRecognitionEngine BackgroundWorker
「No audio輸入被提供給該識別器。使用方法SetInputToDefaultAudioDevice如果將麥克風連接到系統,否則使用SetInputToWaveFile,SetInputToWaveStream或SetInputToAudioStream從執行語音識別預錄音頻」
這裏是在我的DoWork()的代碼功能:
SpeechRecognitionEngine engine = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
engine.SetInputToWaveFile(fname);
engine.LoadGrammar(new DictationGrammar());
engine.BabbleTimeout = TimeSpan.FromSeconds(10.0);
engine.EndSilenceTimeout = TimeSpan.FromSeconds(10.0);
engine.EndSilenceTimeoutAmbiguous = TimeSpan.FromSeconds(10.0);
engine.InitialSilenceTimeout = TimeSpan.FromSeconds(10.0);
BackgroundWorker w = (BackgroundWorker)sender;
while (true)
{
RecognitionResult data = engine.Recognize();
if (data == null)
break;
if (w == null) //our thread died from beneath us
break;
if (!w.IsBusy) //our thread died from beneath us
break;
if (w.CancellationPending) //notice to cancel
break;
w.ReportProgress(0, data.Text);
}
我是l運行多個運行此代碼的BackgroundWorker線程。如果我使用單個線程,我不會看到這個問題。
我還沒有SAPI工作多年,但是從我的頭頂,我記得他們可以跑單了處理COM服務器的,所以如果你試圖讓倍數simultanously工作,這不是默認情況下,你將不得不共享音頻源和進程識別器。 – 2012-02-24 01:00:22
我注意到試圖在BackgroundWorker線程中使用SpeechRecognitionEngine類會導致此異常。 – user1229658 2012-02-24 02:35:47