How to Convert text to speech in C#

By FoxLearn 7/16/2024 8:34:54 AM   14.56K
In a C# Windows Forms Application, you can convert text to speech using the System.Speech.Synthesis namespace, which provides classes for speech synthesis (text-to-speech).

How to create a text to speech c#

Drag and drop Label, Combobox, TextBox and Button controls from the Visual Studio toolbox to your winform, then layout your UI as shown below allows you to select the voice from the combobox, then generate the voice from the text or save the voice to the file.

text to speech in c#

First, you should create the voice variable.

SpeechSynthesizer voice;

Next, Double click on your form, then add code to the Load event handler allows you to create new instance to the voice variable.

private void frmMain_Load(object sender, EventArgs e)
{
    voice = new SpeechSynthesizer();
}

Add the click event handler to the Speak button allows you to create voice from the text

private void btnSpeak_Click(object sender, EventArgs e)
{
    try
    {
        //Select voice
        switch (cboSelectVoice.SelectedIndex)
        {
            case 0:
                voice.SelectVoiceByHints(VoiceGender.NotSet);
                break;
            case 1:
                voice.SelectVoiceByHints(VoiceGender.Male);
                break;
            case 2:
                voice.SelectVoiceByHints(VoiceGender.Female);
                break;
            case 3:
                voice.SelectVoiceByHints(VoiceGender.Neutral);
                break;
            default:
                break;
        }
        voice.SpeakAsync(txtContent.Text);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

This code creates a SpeechSynthesizer object and prompts the user to enter text. Once the user enters text and click the Speak button, the program uses the Speak method of the SpeechSynthesizer object to convert the entered text to speech.

Add the click event handler to the Pause button allows you to pause the speech.

private void btnPause_Click(object sender, EventArgs e)
{
    try
    {
        voice.Pause();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

Add the click event handler to the Resume button allows you to resume the speech.

private void btnResume_Click(object sender, EventArgs e)
{
    try
    {
        voice.Resume();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

To save the voice to file, you can call the SetOutputToWaveStream method.

private void btnSave_Click(object sender, EventArgs e)
{
    try
    {
        //Save text to wav file
        using (SaveFileDialog sfd = new SaveFileDialog())
        {
            sfd.Filter = "Wav files|*.wav";
            sfd.Title = "Save to a wave file";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(sfd.FileName, FileMode.Create, FileAccess.Write);
                voice.SetOutputToWaveStream(fs);
                voice.Speak(txtContent.Text);
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

The c# example robot voice text to speech that helps you integrate into your video, you can also install the natural text to speech, then select the voice to play the demo.

VIDEO TUTORIAL