0

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.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
manicdrummer
  • 161
  • 3
  • 14
  • 1
    What made you think that `Dispose()` would reset the object to its initial state? – Theodoros Chatzigiannakis Aug 12 '15 at 20:57
  • 2
    Dispose basically says "I'm done with this, get rid of it"... You can't say "no, I want it back" after that. The easiest thing to do would be to assign reader to a new instance, `reader = new ...` which will set it to an initial state again. – Ron Beyer Aug 12 '15 at 20:58
  • Remove the `.Dispose()` – thepirat000 Aug 12 '15 at 21:18
  • Thanks for your responses. Ron, the problem with creating a new instance is that I won't be able to access it from outside the procedure in which I'm trying to stop it from playing. That's why I created the global instance "SpeechSynthesizer reader = new SpeechSynthesizer();" This seems to be the only way I can control the speech synth's behavior from anywhere in the app. I know globals aren't necessarily the smartest approach, but I can't find a way to write the code for my needs. – manicdrummer Aug 12 '15 at 21:53

0 Answers0