hello. I've create this windows forms application:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using SpeechLib;
namespace fileSpeakerForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class speaker
{
public string text;
public SpVoice voice = new SpVoice();
public speaker(string _text)
{
text = _text;
}
public void speak()
{
voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault);
}
public void dontSpeak()
{
voice.Pause();
}
public void speakAgain()
{
voice.Resume();
}
}
private void speakBtn_Click(object sender, EventArgs e)
{
OpenFileDialog openDialog = new OpenFileDialog();
openDialog.ShowDialog();
StreamReader sr = new StreamReader(openDialog.FileName);
string textToRead = sr.ReadToEnd();
speaker s = new speaker(textToRead);
Thread t = new Thread(new ThreadStart(s.speak));
t.Start();
}
private void dontBtn_Click(object sender, EventArgs e)
{
//this should stop the spVoice from speaking.
}
}
}
which reads .txt files.
I know that it doesn't handle exceptions, and that it has other bugs, but meanwhile Iwantto focus on the main problem with this.
when the user presses the speak button the application begins to read the file the user haschosen.
while the application is reading(literary, like, speaking) the file, it is unresponsive. The user cannot press the don't speak button because the whole UI is unresponsive. I've tried threading (as you see) but nothing seems to solve this problem.
Any ideas please?
Thanks | | felixrub Saturday, September 26, 2009 2:41 PM | I amusing System.Speech. Synthesis namespace and there is a SpeakAsync method of SpeechSynthesizer object. That method does not lock gui.
Here is a sample
using System.Speech.Synthesis;
......
SpeechSynthesizer s;
public frmSynthesizer()
{
InitializeComponent();
s = new SpeechSynthesizer();
}
private void btnSpeak_Click(object sender, EventArgs e)
{
s.SpeakAsync(txtSubject.Text);
}
private void btnPause_Click(object sender, EventArgs e)
{
s.Pause();
}
private void btnResume_Click(object sender, EventArgs e)
{
s.Resume();
}
private void btnCancel_Click(object sender, EventArgs e)
{
Prompt p = s.GetCurrentlySpokenPrompt();
s.SpeakAsyncCancel(p);
}
- Marked As Answer byfelixrub Saturday, September 26, 2009 3:53 PM
-
| | Tamer Oz Saturday, September 26, 2009 3:30 PM | You are using the old COM interface. .NET 3.0 has a very nice wrapper that will solve your problem. Project + References, select System.Speech. Make your code resemble this: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private SpeechSynthesizer synth = new SpeechSynthesizer(); private void btmPause_Click(object sender, EventArgs e) { synth.Pause(); } private void btnResume_Click(object sender, EventArgs e) { synth.Resume(); } private void btnOpen_Click(object sender, EventArgs e) { if (DialogResult.OK == openFileDialog1.ShowDialog(this)) { synth.SpeakAsync(System.IO.File.ReadAllText(openFileDialog1.FileName)); } } }
Hans Passant.- Marked As Answer byfelixrub Saturday, September 26, 2009 3:53 PM
-
| | nobugz Saturday, September 26, 2009 3:35 PM | I amusing System.Speech. Synthesis namespace and there is a SpeakAsync method of SpeechSynthesizer object. That method does not lock gui.
Here is a sample
using System.Speech.Synthesis;
......
SpeechSynthesizer s;
public frmSynthesizer()
{
InitializeComponent();
s = new SpeechSynthesizer();
}
private void btnSpeak_Click(object sender, EventArgs e)
{
s.SpeakAsync(txtSubject.Text);
}
private void btnPause_Click(object sender, EventArgs e)
{
s.Pause();
}
private void btnResume_Click(object sender, EventArgs e)
{
s.Resume();
}
private void btnCancel_Click(object sender, EventArgs e)
{
Prompt p = s.GetCurrentlySpokenPrompt();
s.SpeakAsyncCancel(p);
}
- Marked As Answer byfelixrub Saturday, September 26, 2009 3:53 PM
-
| | Tamer Oz Saturday, September 26, 2009 3:30 PM | You are using the old COM interface. .NET 3.0 has a very nice wrapper that will solve your problem. Project + References, select System.Speech. Make your code resemble this: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private SpeechSynthesizer synth = new SpeechSynthesizer(); private void btmPause_Click(object sender, EventArgs e) { synth.Pause(); } private void btnResume_Click(object sender, EventArgs e) { synth.Resume(); } private void btnOpen_Click(object sender, EventArgs e) { if (DialogResult.OK == openFileDialog1.ShowDialog(this)) { synth.SpeakAsync(System.IO.File.ReadAllText(openFileDialog1.FileName)); } } }
Hans Passant.- Marked As Answer byfelixrub Saturday, September 26, 2009 3:53 PM
-
| | nobugz Saturday, September 26, 2009 3:35 PM | Thank you, this solves my problem.
By the way, the reason I was using SpeechLib, is because it is used for an example in the 70-536 self-paced training kit. | | felixrub Saturday, September 26, 2009 3:55 PM |
|