Here we will look into creating a simple app to make our app talk.
Introduction
There are circumstances that we might have to make our app more interactive with users, and one of the way is to make our app to talk i.e. it will speak what we write.
This is a simple Text to Speech Converter.
Objective
To attain and understand Text to Speech Conversion.
Using the code
Open MS Visual Studio -> Create New Project -> Windows Application.
Over here in this app we will take on Mutliline Textbox and 4 Buttons.
Button 1 :- To make the app speak the text in Textbox
Button 2 :- To Pause
Button 3 :- To Resume
Button 4 :- To Stop
Button 5 :- To open a file Dialog control
Now first we will have to add the reference of the system.speech dll provided by Microsoft .NET Framework in our references
Right Click Reference Folder Go to .NET Tab and select System.Speech
After that we need to import name space of System.Speech.Synthesis.
This class will provide us the ways and methods to make our app speak.
Now we will create an object of Speech Synthesizer class provided in speech.synthesis and write down the following code
To read the text from notepad we will be using the IO class to read the data from notepad,for that we need to import the System.IO namespace
Now we drag a file dialog control so that we can browse for the text file.
Block of code should be set style as "Code" like below.
// Code behind
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.IO;
namespace text_to_speech
{
public partial class Form1 : Form
{
SpeechSynthesizer reader;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
reader = new SpeechSynthesizer();
btnPause.Enabled = false;
btnResume.Enabled = false;
btnStop.Enabled = false;
xtxtContent.ScrollBars = ScrollBars.Both;
volumebar.Maximum = 100;
speedbar.Maximum = 10;
speedbar.Minimum = -5;
volumebar.Value = 50;
}
//SPEAK TEXT
private void button1_Click(object sender, EventArgs e)
{
reader.Dispose();
if (xtxtContent.Text != "")
{
reader = new SpeechSynthesizer();
reader.Volume = volumebar.Value;
reader.Rate = speedbar.Value;
reader.SpeakAsync(xtxtContent.Text);
label2.Text = "SPEAKING";
btnPause.Enabled = true;
btnStop.Enabled = true;
reader.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(reader_SpeakCompleted);
}
else
{
MessageBox.Show("Please enter some text in the textbox", "Message", MessageBoxButtons.OK);
}
}
void reader_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
{
label2.Text = "IDLE";
}
//PAUSE
private void button2_Click(object sender, EventArgs e)
{
if (reader != null)
{
if (reader.State == SynthesizerState.Speaking)
{
reader.Pause();
label2.Text = "PAUSED";
btnResume.Enabled = true;
}
}
}
//RESUME
private void button3_Click(object sender, EventArgs e)
{
if (reader != null)
{
if (reader.State == SynthesizerState.Paused)
{
reader.Resume();
label2.Text = "SPEAKING";
}
btnResume.Enabled = false;
}
}
private void button4_Click(object sender, EventArgs e)
{
if (reader != null)
{
reader.Dispose();
label2.Text = "IDLE";
btnPause.Enabled = false;
btnResume.Enabled = false;
btnStop.Enabled = false;
}
}
private void button5_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
xtxtContent.Text = File.ReadAllText(openFileDialog1.FileName.ToString());
}
}
}
Output Screen shots:-
Speaking
Pausing
Resume
Stop and Track Bar
Conclusion
Hope this will help any developer with similar requirements.Code updated for Volume bar and Speed