Home > Mobile >  How to detect text to speech is speaking in C#
How to detect text to speech is speaking in C#

Time:08-27

I'm new to C# and I need your help... I am working on a C# Text To Speech program when I press the talk button obviously the text to speech app is going to talk but I want to add a media file of a person who is speaking... What I want is to make a GIF appear when a Text To Speech voice is speaking I want the program to detect when the Text To Speech application starts speaking to make the media file visible and finish speaking to stop the media file from being visible... What I want to do is something similar to the DSpeech program which is the following: https://dspeech.uptodown.com/windows but I have no idea how I do that, I have everything finished and I am missing that, if someone knows I hope they can help me

I have the next code:

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.Speech.Synthesis;

namespace Text_To_Speech
{
    public partial class TextToSpeech : Form
    SpeechSynthesizer reader = new SpeechSynthesizer();
    public TextToSpeech()
    {
            InitializeComponent();
            foreach (var voice in reader.GetInstalledVoices())
            {
                comboBox1.Items.Add(voice.VoiceInfo.Name);
                comboBox1.SelectedIndex = 0;
            }
        }
    }


    private void btSpeak_Click(object sender, EventArgs e)
        {
            if (richTextBox.Text != "")
                {
                    reader = new SpeechSynthesizer();
                    reader.SelectVoice(comboBox1.Text);
                    reader.Rate = (trackBar1.Value);
                    reader.SpeakAsync(richTextBox.Text);
                }
        }
}

CodePudding user response:

Still doubting I fully understand your question, because the answer I am about to give seems so obvious...

private void btSpeak_Click(object sender, EventArgs e)
{
    if (richTextBox.Text != "")
    {
        reader = new SpeechSynthesizer();
        reader.SelectVoice(comboBox1.Text);
        reader.Rate = (trackBar1.Value);

        PictureBox1.Visible = true; //Start
        reader.SpeakAsync(richTextBox.Text);
        PictureBox1.Visible = false; //End
    }
}

"Start" the gif by simple showing the picturebox holding the gif, you may also want to have a look at: Pause a GIF in a form

But my answer doesn't really have anything to do with your question, because the question "how to detect if SpeechSynthesizer is talking?" is IMHO basically defined as you are calling SpeakAsync it talks. End of story.

So, you want to do something prior to it starts talking just execute code before you call the method.

You want to do something after it finishes to talk, just execute code after you call the method.

The only real issue I see is the usage of SpeakAsync which indicates the the code will run asynchronosly, so the "End" won't be the next line of code but some time in the future.

To overcome this simply register to the SeapkCompleted event, like this:

reader = new SpeechSynthesizer();
reader.SpeakCompleted  = Reader_SpeakCompleted;

private void Reader_SpeakCompleted(object sender, SpeakCompletedEventArgs args)
{
    PictureBox1.Visible = false; //End
}

You should have a look at all the events SpeechSynthesizer is offering

https://docs.microsoft.com/en-us/dotnet/api/system.speech.synthesis.speechsynthesizer?view=netframework-4.8#events

Event Name Event Description
BookmarkReached Raised when the SpeechSynthesizer encounters a bookmark in a prompt.
PhonemeReached Raised when a phoneme is reached.
SpeakCompleted Raised when the SpeechSynthesizer completes the speaking of a prompt.
SpeakProgress Raised after the SpeechSynthesizer speaks each individual word of a prompt.
SpeakStarted Raised when the SpeechSynthesizer begins the speaking of a prompt.
StateChanged Raised when the state of the SpeechSynthesizer changes.
VisemeReached Raised when a viseme is reached.
VoiceChange Raised when the voice of the SpeechSynthesizer changes.
  • Related