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

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

问题描述

我在一个项目,该项目会说话浏览网页page.Browser的工作内容是由我使用的WebControl的。我使用的SAPI语音引擎。我想强调在网页中的行同时读取槽SpVoice.speak。但问题是,如果我使用异步的方式对她说话的方法,那么只有在网页的最后一行越来越突出,因为循环不会等待语音来完成。因而它发生如此之快,只有最后一行被示为highlighted.Highlight方法是通过使用参考微软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:

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; }

    }



私人无效Read_ButtonSpkBAR_Click(对象发件人,EventArgs五)
{
//调用获取源代码
gettingSourceCode();

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);




                }
            }
        }

现在如果我叫了sound_object.speak()的同步方式是sound_object.Speak(splitSentences [I]);然后循环等待语音来完成,但我不知道为什么它没有表现出突出显示的行。该软件得到绞刑而speaking.That意味着web浏览器一边讲不会做任何事情,但讲程序,当时工作正常。

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.

有关检查,我把一个消息循环中,看到该行得到强调如果确定按钮,循环等待被推这些URL的亮点。但是,这不是一个好主意,在所有按下确定按钮,每行。
因此,谁能请帮助我,这是什么问题以及如何使用SAPI或任何其他语音引擎以有效的方式,这样我可以阅读和完全彰显没有得到绞死的浏览器..... ...

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........

推荐答案

所有的句子弄成一个字符串,并调用再说这一次。
低于替代UR for循环:

get all the sentences into one string, and call the speak just once. replace ur for loop with below:

string str1 = "";
for (int i = 0; i < splitSentences.Length; i++) 
{ 
     str1 += highlight(splitSentences[i]); 
}
sound_object.Speak(str1, SpeechLib.SpeechVoiceSpeakFlags.SVSFlagsAsync);



希望这有助于!

hope this helps!

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

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