How To Listen the contents of the Text file(.txt) using the Java Speech Synthesizer.

Today I am posting on the Code That will allow you to listen to the contents of the .txt file or a text file..

The main concept that has been used here is the same as the Synthesizer You can make this by jus making the some changes in to the previous code.

All the requirements are as previous that has shown in the post of the Speech Synthesizer (Previous post), now here i have used the Applet that will fetch the Text file from the Directory in which the Class file reside. So you have to pass the name of the file you want to read and that should be inside that folder, So it will fetch that Text file and fetch all the data in the Text Area by importing the AWT package.

so the basic code is as follows:-

import java.applet.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.speech.Central;
import javax.speech.EngineList;
import javax.speech.EngineCreate;
import javax.speech.synthesis.SynthesizerModeDesc;
import javax.speech.synthesis.Synthesizer;
import javax.speech.synthesis.Voice;
import java.awt.TextArea;
import java.net.*;
import java.io.*;
import com.sun.speech.freetts.jsapi.FreeTTSEngineCentral;
import java.util.Locale;
//import java.awt.Event;
@SuppressWarnings(“serial”)
public class voisedemo extends Applet implements ActionListener
{
public Synthesizer synth;
private static Voice kevinHQ;
//TextField t1;
TextArea t1;
public void init()
{
Button b1 = new Button(“press me”);
add(b1);
t1 = new TextArea(20,80);
add(t1);
//provide the name of the file you want to open and that sholud be inside that directory that contains the .class file of //this source code
readFile(“write.txt”);
b1.addActionListener(this);
add(t1);
}
public void readFile(String f) {
try {
String aLine = “”;
URL source = new URL(getCodeBase(), f);
BufferedReader br =
new BufferedReader
(new InputStreamReader(source.openStream()));
while(null != (aLine = br.readLine())) {
t1.append(aLine);
t1.append(“\n”);
System.out.println(aLine);
}
br.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
public void start()
{
}
public void actionPerformed(ActionEvent e)
{
try {
// create SynthesizerModeDesc that will match the FreeTTS Synthesizer
// System.out.print( ”  Loading voice…” );
setKevinHQ(new Voice(“Hitesh”,
Voice.AGE_NEUTRAL,
Voice.GENDER_MALE,
null ));
System.setProperty(“freetts.voices”, “com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory”);
SynthesizerModeDesc modeDesc = new SynthesizerModeDesc(
null,
“general”, /* use “time” or “general” */
Locale.US,
Boolean.FALSE,
null);
FreeTTSEngineCentral central = new FreeTTSEngineCentral();
Synthesizer synthesizer = null;
synthesizer = Central.createSynthesizer( modeDesc );
EngineList list = central.createEngineList(modeDesc);
if (list.size() > 0) {
EngineCreate creator = (EngineCreate) list.get(0);
synthesizer = (Synthesizer) creator.createEngine();
}
if (synthesizer == null) {
System.err.println(“Cannot create synthesizer”);
System.exit(1);
}
//get ready to speak
synthesizer.allocate();
synthesizer.resume();
// speak the content
String s1 = t1.getText();
//speakPlainText() is the method that take the String as the Argument and will speak.
synthesizer.speakPlainText(s1, null);
// wait until speaking is done and clean up
synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
synthesizer.deallocate();
} catch (Exception eq) {
eq.printStackTrace();
}
}
/**
* @param kevinHQ the kevinHQ to set
*/
public static void setKevinHQ(Voice kevinHQ) {
voisedemo.kevinHQ = kevinHQ;
}
/**
* @return the kevinHQ
*/
public static Voice getKevinHQ() {
return kevinHQ;
}
public void paint(Graphics g)
{
}
}
/*
<applet code=”voisedemo” width=1000 height=1000>
</applet>
*/
Thank you and if you have any Questions in this, than feel free to ask me and post your comments, the following is the output of this code that is running well

Text file Listener
Text file Listener

Thank you for being here..! njoy the Day..!  Stay Hungry Stay foolish..! @};–

Advertisements

13 thoughts on “How To Listen the contents of the Text file(.txt) using the Java Speech Synthesizer.

  1. how are you!This was a really splendid Topics!
    I come from china, I was fortunate to come cross your Topics in google
    Also I learn much in your theme really thank your very much i will come again

  2. I have an error. What does it mean?

    Exception in thread “Thread-5” java.lang.IllegalArgumentException: No line matching interface SourceDataLine supporting format PCM_SIGNED 8000.0 Hz, 16 bit, mono, 2 bytes/frame, big-endian is supported.
    at javax.sound.sampled.AudioSystem.getLine(Unknown Source)
    at com.sun.speech.freetts.audio.JavaStreamingAudioPlayer.openLine(JavaStreamingAudioPlayer.java:194)
    at com.sun.speech.freetts.audio.JavaStreamingAudioPlayer.begin(JavaStreamingAudioPlayer.java:410)
    at com.sun.speech.freetts.relp.LPCResult.playWaveSamples(LPCResult.java:505)
    at com.sun.speech.freetts.relp.LPCResult.playWave(LPCResult.java:403)
    at com.sun.speech.freetts.relp.AudioOutput.processUtterance(AudioOutput.java:65)
    at com.sun.speech.freetts.Voice.runProcessor(Voice.java:595)
    at com.sun.speech.freetts.Voice.outputUtterance(Voice.java:536)
    at com.sun.speech.freetts.Voice.access$200(Voice.java:81)
    at com.sun.speech.freetts.Voice$1.run(Voice.java:496)

  3. hello i m student developing java applications for fun can u tell me from where i can download synthesis package?
    plz reply

  4. where can i get the package javax.speech and how do i install it in my computer so when i import it it doesn’t show an error.

    1. You have to download the jar file called the Sphinix 1.4 or any other version.. and after that you have to add that jar file as the external jar file in your project. so that your class would be able to find the class javax.speech.. and thats it.. its done.. let me know if u find any further probs… Bye Take care..

  5. hi hitesh, i watched ur video in youtube. That was grt. I’m also workin in a prjct speech to text in java. can u jus help me out and provide me the code of speech recognition which is on youtube.

  6. hii we are working on a project of text to speech conversion in java.your code helped us a lot
    but we are not getting the voice as output can u help us?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s