Development - C# Application - Text to speech

Words
471
Reading
3 min
Listen
Play
9y

Create Text to Speech/Converter Sample using C# step by step.

First we open microsoft visual studio program

We do the settings in the first 4 images in order

1.png
2.png
3.png
4.png


We put 1 label and 1 combo box on the form screen
5.png


Click once on the combo box and click on edit items
6.png


In the window that opens, type the following and click OK
NotSet - Male - Female - Neutral
7.png


Then, on the form screen, take 1 text box and make it look like you see the settings
8.png


Now let's put 4 buttons on my form screen
9.png


Then click button 1 and do the settings in the picture
10.png
11.png


Do the same for button 2 3 and 4 .
Briefly;
button2 : Text = Resume and Name = btnResume
button3 : Text = Pause and Name = btnPause
button4 : Text = Speak and Name = btnSpeak

Result;
12.png


Click once in the text box and make it look like the image
Name = txtContent
13.png


Go to Form1.cs on the right, right click and choose Rename, and name it frmMain
14.png
15.png


Right-click Preferences, select Add references
16.png
Down to select system.speech and press ok
17.png


Double-click the form screen to go to the code screen, and do as you see in the image.
Codes;
using System.Speech.Synthesis;
SpeechSynthesizer voice;
voice = new SpeechSynthesizer();
18.png


We go back to the form screen and double-click on the speak button.
19.png


And we write our codes
private void btnSpeak_Click(object sender, EventArgs e)
{
try
{

         }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

20.png

We also do the same for pause, resume and save buttons
21.png
22.png
23.png


Return to the form screen and click once on the combo box to make the setting in the image.
24.png


Then double-click on the talk button and come to the code screen and make it look like in the picture.
Codes;
private void btnSpeak_Click(object sender, EventArgs e)
{
try
{
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);
24.png


Then come and edit btnPause_Click
try
{
voice.Pause();
}
26.png


Then come and edit btnResume_Click
try
{
voice.Resume();
}
27.png


Then come and edit btnSave_Click
private void btnSave_Click(object sender, EventArgs e)
{
try
{
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);
}
}
}
28.png


**And here is the result :) **
https://www.youtube.com/watch?v=UiJUUN2oChk
I hope this is useful for you so you can improve yourself by working this way

If there is a problem, you can write a comment under this topic.

Project result : https://github.com/ShanksTaicho/C-Text-to-speech



Posted on Utopian.io - Rewarding Open Source Contributors

Development - C# Application - Text to speech | Ecency