Click here to Skip to main content
15,913,332 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
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; }

    }


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




                }
            }
        }



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........
Posted
Comments
Sergey Alexandrovich Kryukov 7-Mar-11 14:05pm    
I'm curious, why not referencing System.Speech assembly and using System.Speech.Synthesis?
--SA
atanuCSE 8-Mar-11 8:57am    
I have tried that before.Result was the same.

1 solution

Placing an
VB
Application.DoEvents()
?
 
Share this answer
 
Comments
atanuCSE 9-Mar-11 11:24am    
Bro Thank you sooooo much :-) I can't explain how much you helped me! Now it's highlighting the sentence which just being end reading. at least there is no need of any message box.thank you once again :-)
Any more efficient ways from anyone?? please let me know guys!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900