Click here to Skip to main content
13,190,404 members (51,099 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: , +
Hello I have developed a program to speak the contents of a web page.
Here is the code i do this with:

 
        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:

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:


 
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:


        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 24-Nov-14 10:00am
Updated 24-Nov-14 10:08am
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi,

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

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 :)
  Permalink  
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 :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

SP.Speak("Hello world!", SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak | SpeechVoiceSpeakFlags.SVSFlagsAsync);
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.171016.2 | Last Updated 25 Nov 2014
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100