积极答复者
各位大虾,帮我看看这个Speech语音引擎的问题吧。

问题
-
想要使用Speech做一个语音识别的程序,把语音命令录入到文本框中,先用Winform做,去网上找了一段代码,发现用不了,错误如下:
Interop Type'SpeechLib.SpSharedRecoContextClass'cannot be embeded.Use the application interface instead,
程序源代码如下,求解释啊,谢谢!
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Speech.Recognition; using System.Speech.Synthesis; using System.Configuration; using System.IO; using Microsoft.Win32; using System.Collections; using SpeechLib; namespace SpeechRecognition { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click_1(object sender, EventArgs e) { SpRecognition cc = new SpRecognition(); cc.MessageBegin(); cc.BeginRec(textBox1); //Fan:把内容输入textbox1 } private void button2_Click_1(object sender, EventArgs e) { SpRecognition cc = new SpRecognition(); cc.MessageEnd(); cc.CloseRec(); } public class SpRecognition { private static SpRecognition _Instance = null; private SpeechLib.ISpeechRecoGrammar isrg; private SpeechLib.SpSharedRecoContextClass ssrContex = null; private System.Windows.Forms.Control cDisplay; //fan:用来显示语音转化后的文本 public System.Windows.Forms.TextBox textbox; //fan:增加textbox在SpRecognition 类 public SpRecognition() { ssrContex = new SpSharedRecoContextClass(); isrg = ssrContex.CreateGrammar(1); SpeechLib._ISpeechRecoContextEvents_RecognitionEventHandler recHandle = new _ISpeechRecoContextEvents_RecognitionEventHandler(ContexRecognition); ssrContex.Recognition += recHandle; } public void BeginRec(Control tbResult) { isrg.DictationSetState(SpeechRuleState.SGDSActive); cDisplay = tbResult; //cDisplay.Text = "dddddddddd"; 测试,可以成功在textbox1那里显示出来 } public static SpRecognition instance() { if (_Instance == null) _Instance = new SpRecognition(); return _Instance; } public void CloseRec() { isrg.DictationSetState(SpeechRuleState.SGDSInactive); } private void ContexRecognition(int iIndex, object obj, SpeechLib.SpeechRecognitionType type, SpeechLib.ISpeechRecoResult result) { cDisplay.Text += result.PhraseInfo.GetText(0, -1, true); } public void MessageBegin() { textbox = new TextBox(); textbox.Text = "Notice :this time ,it Begin recoginse"; MessageBox.Show(textbox.Text); } public void MessageEnd() { textbox = new TextBox(); textbox.Text = "Notice :this time ,it End recoginse"; MessageBox.Show(textbox.Text); } } } }
答案
-
不是非常清楚你使用的语音识别软件。我是推荐你使用Microsoft的内置的:参考示例代码——
首先,新建一个C#的Windows Application工程SpeechApp,在开发环境的右边的解决方案管理器(Solution Explorer)中,添加DotNetSpeech对象库。右键点击"Reference"(参考),选择"Add Reference"(添加参考),在弹出的文件选择对话框中找到D:\Program Files\Common Files\Microsoft Shared\Speech\目录下面找到SAPI.dll,
以下为引用的内容:
//朗读
private void buttonSynthesis_Click(object sender, System.EventArgs e)
{
try
{
SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
SpVoice Voice = new SpVoice();
Voice.Speak(this.textBoxText.Text, SpFlags);
}
本文来自CSDN博客,更多请参考:http://blog.csdn.net/yuanchunze/archive/2009/01/22/3850835.aspx
如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com
下载MSDN桌面工具(Vista,Win7)
下载Technet桌面小工具(Vista,Win7)
慈善点击,点击此处- 已建议为答案 mazhou 2011年5月10日 10:33
- 已标记为答案 mldarkModerator 2011年5月10日 18:10
-
其实只用两句话就可以读文本了。
Type type = Type.FromProgId("SAPI.SpVoice");
dynamic spVoice = Activator.CreateInstance(type);
spVoick.Speak("There are many public holidays in this May.");
Mark Zhou- 已标记为答案 mldarkModerator 2011年5月10日 18:10
全部回复
-
不是非常清楚你使用的语音识别软件。我是推荐你使用Microsoft的内置的:参考示例代码——
首先,新建一个C#的Windows Application工程SpeechApp,在开发环境的右边的解决方案管理器(Solution Explorer)中,添加DotNetSpeech对象库。右键点击"Reference"(参考),选择"Add Reference"(添加参考),在弹出的文件选择对话框中找到D:\Program Files\Common Files\Microsoft Shared\Speech\目录下面找到SAPI.dll,
以下为引用的内容:
//朗读
private void buttonSynthesis_Click(object sender, System.EventArgs e)
{
try
{
SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
SpVoice Voice = new SpVoice();
Voice.Speak(this.textBoxText.Text, SpFlags);
}
本文来自CSDN博客,更多请参考:http://blog.csdn.net/yuanchunze/archive/2009/01/22/3850835.aspx
如果你有其它意见或私下交流,请发送邮件到:maledong@qq.com
下载MSDN桌面工具(Vista,Win7)
下载Technet桌面小工具(Vista,Win7)
慈善点击,点击此处- 已建议为答案 mazhou 2011年5月10日 10:33
- 已标记为答案 mldarkModerator 2011年5月10日 18:10
-
其实只用两句话就可以读文本了。
Type type = Type.FromProgId("SAPI.SpVoice");
dynamic spVoice = Activator.CreateInstance(type);
spVoick.Speak("There are many public holidays in this May.");
Mark Zhou- 已标记为答案 mldarkModerator 2011年5月10日 18:10