Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello I have developed a program to speak the contents of a web page.
Here is the code i do this with:

C#
public void SpeakSynthText(String TextToSpeak, bool SpeechRecognitionEnable)
{
    #region SpeechSynth
    if (SpeechRecognitionEnable == true)
    {
        //starts the speech engine
        this.Start();
    }


    // Initialize a new instance of the SpeechSynthesizer.
    SpeechSynthesizer synth = new SpeechSynthesizer();
    if (Boolean.Parse(SpeechSaveEnabled) == true)
    {
        //FolderDialogSaveSpeechToDirectory();
        SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        saveFileDialog1.Filter = "wave files (*.wav)|*.wav|All files (*.*)|*.*";
        saveFileDialog1.FilterIndex = 1;
        saveFileDialog1.RestoreDirectory = true;

        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {

            TB.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
            WaveFileLoc = saveFileDialog1.FileName;
            synth.SetOutputToWaveFile(WaveFileLoc);
            synth.Rate = -1;

        }


    }
    // Select a voice that matches a specific gender.
    synth.SelectVoiceByHints(VoiceGender.Female);
    // Speak a text string synchronously and play back the output file.


    synth.Speak(TextToSpeak);
    synth.Dispose();
    //synth.SetOutputToWaveFile();
    verbalcommandmode = "true";
    if (Boolean.Parse(SpeechSaveEnabled) == true)
    {
        System.Media.SoundPlayer m_SoundPlayer =
        new System.Media.SoundPlayer(@WaveFileLoc);
        m_SoundPlayer.Play();
        m_SoundPlayer.Dispose();
    }
    #endregion
}


when i want to speak the contents of a web page i pass the arguments of:

C#
this.SpeakSynthText(WIKI.Document.Body.InnerText, false);


now when i run this the UI freezes up until its done speaking, so i was told that i would have to run the function on a thread separate of the UI thread.
So i read all the articles i could find on codeproject and this is what i got:


C#
private void SpeakPageText_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(RunInThread));
            t.Start(); 

            //WIKI.Document.Body.InnerText

        }


this function(sorry i keep calling them functions i am used to coding in python) is called when i click the button associated with this method
when i run this method it is SUPPOSED to run the RunInThread method in a new thread. The RunInThread method goes like this:


C#
public void RunInThread()
{

    //Option 1

    try
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new EventHandler(delegate { this.SpeakSynthText(WIKI.Document.Body.InnerText, false); }));
        }
    }

    catch (InvalidOperationException oex)
    {
        MessageBox.Show(oex.Message);
    }

}


i have a feeling that it isn't running RunInThread on a separate thread but instead on the main thread, so how do run this method on a new thread?
or if that won't work how can i speak text with the speech synthesizer but not have the UI freeze up?
All of your help is very much appreciated.
Posted
Updated 24-Nov-14 10:08am
v3

Hi,

You don't need separate thread. All you need to call method:

C#
synth.SpeakAsync(...);


Few links:
http://msdn.microsoft.com/en-us/library/system.speech.synthesis%28v=vs.100%29.aspx[^]
http://msdn.microsoft.com/en-us/library/system.speech.synthesis.speechsynthesizer%28v=vs.100%29.aspx[^]
http://msdn.microsoft.com/en-us/library/ms586891%28v=vs.100%29.aspx[^]

And you have few events to subscribe:
http://msdn.microsoft.com/en-us/library/system.speech.synthesis.speechsynthesizer.speakstarted%28v=vs.100%29.aspx[^]
http://msdn.microsoft.com/en-us/library/system.speech.synthesis.speechsynthesizer.speakprogress%28v=vs.100%29.aspx[^]
http://msdn.microsoft.com/en-us/library/system.speech.synthesis.speechsynthesizer.speakcompleted%28v=vs.100%29.aspx[^]

[Update]
Two years ago I wrote post about MS Text-To-Speech. Article is in polish language but source code is in english. You can download source code here:
http://kozub.net.pl/download/sources/SayIT%20source.zip[^]
Binary files here:
http://kozub.net.pl/download/programs/SayIT.zip[^]
Blog post is located here:
http://kozub.net.pl/2012/04/01/sayit-czyli-jak-sprawic-aby-komputer-przemowil/[^]

Sample project allows you to play all or selected text from TextBox using default windows sound output. You can select voice from voices available on your system, get info about selected voice and control playback speed rate and volume. It can save output as WAV files as well. You can configure WAV file options such as bits per sample, channels and quality. Also it displays informations about playback progress, position etc.

I hope you find this useful :)
 
Share this answer
 
v4
Comments
MasterCodeon 25-Nov-14 12:04pm    
Could you send me the source for that text to speech article you did(i can't access the link)if you could that would be great because i need a way to speak the selected text not just all text.
if you can just send me a PM with it or something.
please and thank you.
MasterCodeon 25-Nov-14 12:04pm    
Oh and by the way it worked like a charm thanks again!
Marcin Kozub 25-Nov-14 12:36pm    
I'm glad that I helped you :)
SP.Speak("Hello world!", SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak | SpeechVoiceSpeakFlags.SVSFlagsAsync);
 
Share this answer
 
Comments
MasterCodeon 24-Nov-14 16:12pm    
could you elaborate please.

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