SAPI或(文本到语音)…C#的同步问题 [英] Synchronization Problem for SAPI or (text to speech ) … C#

查看:102
本文介绍了SAPI或(文本到语音)…C#的同步问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目,该项目说的是浏览的网页的内容.浏览器是由我使用WebControl制作的.我正在将SAPI用于语音引擎.我想在阅读SpVoice.speak低谷时突出显示网页中的这一行.但是问题是 如果我以异步方式使用此语音方法,则会仅突出显示网页的最后一行,因为循环不会等待语音完成.因此,它发生得如此之快,以至于只有最后一行显示为高亮. 参考Microsoft mshtml.代码: 

I'm working on a project which will speak the content of browsed web page.Browser is made by me using WebControl. I'm using SAPI for speech engine. I wanted to highlight the line in web page while reading that trough SpVoice.speak. But the problem is that if I use this speak method in Asynchronous way,then only last line of the web page getting highlighted,because the loop doesn't wait for the voice to complete. Thus it happens so fast that only last line is shown as highlighted.Highlight method is using a reference Microsoft mshtml. CODE:  

< code>

<code>

SpeechLib.SpVoice sound_object = new SpeechLib.SpVoice();
bool highlight(string senten)
{
if (senten != null)
{
IHTMLDocument2 doc = (IHTMLDocument2)GetCurrentBrowser().Document.DomDocument;
IHTMLSelectionObject sel = (IHTMLSelectionObject)doc.selection;
IHTMLTxtRange rng = (IHTMLTxtRange)sel.createRange();
rng.collapse(false);
if (rng.findText(senten, 1000000, 0))
{
rng.select();
return true;
}
else
{
return false;
}

}
else
{ return false; }

}

private void Read_ButtonSpkBAR_Click(object sender, EventArgs e) { //call for getting sourceCode gettingSourceCode();

if (highlightToolStripMenuItem.Checked == true)
{

if (PAUSE)
{
sound_object.Resume();
PAUSE = false;
}
else
{
sound_object.Rate = tempoRate;
sound_object.Volume = volume;


string[] splitSentences = Regex.Split(SourceCode, @"(?<=['""A-Za-z0-9][\.\!\?\u2424])\s+(?=[A-Z])");



for (int i = 0; i < splitSentences.Length; i++)
{


highlight(splitSentences[i]);
//MessageBox.Show(splitSentences[i]);

sound_object.Speak(splitSentences[i],SpeechLib.SpeechVoiceSpeakFlags.SVSFlagsAsync);




}
}
}

</code>


Now if I call the sound_object.speak() in a Synchronize way which is sound_object.Speak(splitSentences[i]); then the loop wait for the voice to complete,but I don't know why it didn't show highlighted line. The software get hanged while speaking.That means WebBrowser doesn't do anything while speaking,but the speaking procedure works fine at that time.

For checking the highlight I put a messagebox inside of loop and seen that the lines get highlighted if the loop wait for the "OK" button to be pushed of the messagebox.But this isn't a good idea at all to push "OK" button for each line. So can anyone please help me that what is the problem and how can I use the SAPI or any other speech engine in a efficient way, so that I can read and highlight altogether without getting the browser hanged........

推荐答案

SpVoice.speak异步呼叫: 仅网页的最后一行被突出显示,因为在异步调用时, 它立即返回.

SpVoice.speak Asynchronous Call  :  Only the last line of the web page is getting highlighted because when called asynchronously, it returns immediately.

WaitUntilDone和SpeakCompleteEvent方法可用于确保将口语突出显示为文本到语音 流程操作到位.

 The WaitUntilDone and SpeakCompleteEvent methods can be used to ensure that the spoken words are highlighted as the text to speech process operation is in place.

对于代码示例请参考mdsn文章

For code sample please refer to the mdsn article

http://msdn.microsoft.com/en-us/library/ms723609(VS.85).aspx

SpVoice.speak同步通话: 这不是首选方法,因为在语音提示时会阻止应用程序的执行 说话,用户就被有效锁定了.

SpVoice.speak synchronous Call  :  This is not a preferred method because the application's execution is blocked while the voice speaks, and the user is effectively locked out.

因此,如果您想要启动文本到语音的过程,并继续您在讲话时需要做的事情(突出显示文本), 您需要Speak方法是异步的.

So if you want to kick off the text to speech process and continue with what you need to do ( highlighting the text ) while it's speaking, you'll need the Speak method to be asynchronous.


这篇关于SAPI或(文本到语音)…C#的同步问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆