我想尝试语音识别来控制程序。我用 C# 编写了测试程序,当我调试它时,每次都会发生错误 -
System.Runtime.InteropServices.COMException (0x80004005): Calling part of COM return error HRESULT E_FAIL.*
in System.Speech.Recognition.RecognizerBase.Initialize(SapiRecognizer recognizer, Boolean inproc)
in System.Speech.Recognition.SpeechRecognitionEngine.get_RecoBase()
in System.Speech.Recognition.SpeechRecognitionEngine.LoadGrammar(Grammar grammar)
看起来错误是由于engine.LoadGrammar(new DictationGrammar());
我的笔记本上我安装了 CZECH OS Vista 引起的,这可能是语音识别语言与操作系统语言不同的问题。
有没有办法在非英语操作系统中使用 system.speech 进行开发,或者我在某些步骤中错了?语言没有问题,我想用英语进行语音识别,但是我无法获得英语 Vista 或 MUI 语言包。
完整代码如下。
非常感谢!
using System;
using System.Windows;
using System.Speech.Recognition;
namespace rozpoznani_reci_WPF
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
SpeechRecognitionEngine engine = new SpeechRecognitionEngine();
try
{
engine.LoadGrammar(new DictationGrammar());
engine.SetInputToDefaultAudioDevice();
engine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
}
catch(Exception e)
{
//MessageBox.Show(e.ToString());
textBox1.Text = e.ToString();
}
}
void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
if (e.Result != null)
{
textBox1.Text = e.Result.Text + " ";
}
}
}
}