I'm working on a text to speech app using the Microsoft Speech Engine and SAPI voices and for the most part, it's working the way it should.
First, here's my using statements:
using System;
using System.Globalization;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Synthesis;
OK, so I type some text into a text box, click the Play/Pause button and it reads the text aloud. The Play/Pause button seems to be working just fine.
Here's the code for the Play/Pause button:
private void btnPlayPause_Click(object sender, EventArgs e)
{
if (btnPlayPause.Text == "Pause")
{
reader.Pause();
btnPlayPause.Text = "Play";
}
else if (btnPlayPause.Text == "Play")
{
if (txtTextToSpeak.Text == "")
MessageBox.Show("Please type some text.", "OOPS!", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
if (reader.State == SynthesizerState.Paused)
reader.Resume();
else
{
reader.SelectVoice(cmbxVoices.Text);
reader.SpeakAsync(txtTextToSpeak.Text);
btnPlayPause.Text = "Pause";
}
}
}
}
And here's the code for the Stop button's click event:
private void btnStop_Click(object sender, EventArgs e)
{
reader.Pause();
reader.Dispose();
btnPlayPause.Text = "Play";
glbstrText2BSpoken = "";
}
Here's the problem: When I click the Stop button, I want the reader to reset itself so that the next time I click the Play button, it reads the text from the beginning. I tried reader.Dispose();" in the Stop button's event handler, but the app crashes with the error "Cannot access a disposed object." This occurs when I click the Stop button and then click the Play button.