Frank Ryan

Frank Ryan

  • NA
  • 4
  • 2k

winform application help required with voice synthesis

Oct 20 2015 6:27 AM
hi,

how can i have a stop button on my form to stop the voice synthesis from speaking and then it clears the textbox -- that part i have done but when i add new text to the textbox and hit startReading the errorthing on visual studio make my program stop running everytime ? here is my code so far hope you can understand it someone ?
 
 this is the errorcode it says "An unhandled exception of type 'System.ObjectDisposedException' occurred in System.Windows.Forms.dll"
 
here is my code
 
using System;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.IO;
using MetroFramework.Forms;
using System.Drawing;
using System.Threading;
using System.Text;
using System.Linq;
namespace TextReader
{
public partial class Form1 : MetroForm
{
public Form1()
{
InitializeComponent();
// list installed voices in a listbox (lstVoices)
//foreach (object obj in ss.GetInstalledVoices())
//{
// var voice = (InstalledVoice)obj;
// lstVoices.Items.Add(voice.VoiceInfo.Name);
//}
}
SpeechSynthesizer ss = new SpeechSynthesizer();
//SpeechRecognizer sr = new SpeechRecognizer();
// Select the US English voice.
private void Form1_Load(object sender, EventArgs e)
{
string[] comArgs = Environment.GetCommandLineArgs();
foreach (string s in comArgs)
{
if (comArgs.Contains("/r"))
{
tbReading.Text = (s);
btnRead.PerformClick();
}
if (comArgs.Contains("/f"))
{
tbReading.Text = (s);
cbVoice.Text = "Female";
btnRead.PerformClick();
}
}
}
private void btnRead_Click(object sender, EventArgs e)
{
if (cbVoice.Text == "Male")
{
ss.SelectVoiceByHints(VoiceGender.Male, VoiceAge.Child);
}
else if(cbVoice.Text == "Female")
{
ss.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Senior);
}
else
{
ss.SelectVoiceByHints(VoiceGender.Male, VoiceAge.Senior);
}
// to find out the speech rate currently set by adding a string
//and call SpeakAsync("With a string of text to test rate")
string rate = cbRate.Text; // set a string from tbRate.Text box
int numberRate = Convert.ToInt32(rate); // convert string to int to be called in the next line
ss.Rate = numberRate; // set the rate of speech
// set the volume
string vol = cbVol.Text;
int numberVol = Convert.ToInt32(vol);
ss.Volume = numberVol;
btnPause.Enabled = true;
btnStop.Enabled = true;
ss.SpeakAsync(tbReading.Text); // Start Speaking the text at the rate previously specified in numberRate
btnRead.Hide();
}
private void btnStopReading_Click(object sender, EventArgs e)
{
tbReading.Clear();
ss.SpeakAsyncCancelAll();
ss.Dispose();
Form1 F1 = new Form1();
F1.Close();
F1.Show();
}
public void pause()
{
ss.Pause();
}
public void resume()
{
ss.Resume();
}
private void btnPause_Click(object sender, EventArgs e)
{
pause();
btnPause.Hide();
btnResume.Show();
}
private void cbRate_DropDownClosed(object sender, EventArgs e)
{
//cbVol.Enabled = true;
}
private void cbVoice_DropDownClosed(object sender, EventArgs e)
{
if(cbVoice.Text == "Female")
{
tbReading.BackColor = Color.Blue;
}
else if (cbVoice.Text == "Male")
{
tbReading.BackColor = Color.DeepPink;
}
else
{
tbReading.BackColor = Color.Crimson;
}
}
private void btnClear_Click(object sender, EventArgs e)
{
}
private void btnPaste()
{
tbReading.Text = Clipboard.GetText();
}
private void tbReading_MouseDoubleClick(object sender, MouseEventArgs e)
{
btnPaste();
}
private void tbReading_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
// Determine whether the key entered is the F1 key. Display help if it is.
if (e.KeyCode == Keys.F1)
{
// Display a pop-up help topic to assist the user.
MessageBox.Show("Enter your first name", "F1 Pressed", MessageBoxButtons.OK);
}
}
private void label2_Click(object sender, EventArgs e)
{
btnPaste();
}
private void btnOpen_Click(object sender, EventArgs e)
{
btnClear.PerformClick();
OpenFileDialog open = new OpenFileDialog();
open.ShowDialog();
tbReading.Text = open.FileName;
StreamReader sr = new StreamReader(open.FileName);
tbReading.Text = sr.ReadToEnd();
sr.Close();
}
private void metroTextButton1_Click(object sender, EventArgs e)
{
resume();
}
public void btnResume_Click(object sender, EventArgs e)
{
// set the volume from cbVol.Text ComboBox
string vol = cbVol.Text;
int numberVol = Convert.ToInt32(vol);
ss.Volume = numberVol;
int currVol = numberVol;
string rate = cbRate.Text; // set a string from tbRate.Text box
int numberRate = Convert.ToInt32(rate); // convert string to int to be called in the next line
ss.Rate = numberRate; // set the rate of speech
btnPause.Enabled = true;
btnStop.Enabled = true;
ss.SpeakAsync(tbReading.Text); // Start Speaking the text at the rate previously specified in numberRate
btnRead.Hide();
ss.Resume();
btnResume.Hide();
btnPause.Show();
if(ss.Volume != currVol)
{
}
}
private void cbVol_TextChanged(object sender, EventArgs e)
{
}
private void cbVol_TextChanged_1(object sender, EventArgs e)
{
//// set the volume from cbVol.Text ComboBox
//string vol = cbVol.Text;
//int numberVol = Convert.ToInt32(vol);
//ss.Volume = numberVol;
//int currVol = numberVol;
//ss.Volume = currVol;
}
}
}