Want to create your own reader for your .txt files using java .If yes then you are on the right track.Here you see how to use Free TTS ,A free API written entirely using Java TM .
Requirements
In order to create this reader app you need Free TTS 1.2.2 API you could download it from here
What the program do ?
The program given below will prompt you to enter the path of a .txt file in your computer and then it will read the file in a natural human voice for you .
Reader.java
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; import com.sun.speech.freetts.Voice; import com.sun.speech.freetts.VoiceManager; public class Reader { // Some available voices are (kevin, kevin16, alan) private static final String VOICE_NAME_KEVIN = "kevi"; private final Voice voice; public Reader() { VoiceManager vm = VoiceManager.getInstance(); voice = vm.getVoice(VOICE_NAME_KEVIN); voice.allocate(); } public void speak(File file) { if(file != null) { FileInputStream inputStream = null; try { inputStream = new FileInputStream(file); voice.speak(inputStream); } catch (FileNotFoundException e) { voice.speak("Hey!it seems that the file does not exists."); System.out.println(e.getMessage()); e.printStackTrace(); } } } public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.print("Enter full path to the file(.txt):-"); String path=sc.nextLine(); File file = new File(path); Reader r= new Reader(); // Passing file object as argument r.speak(file); System.out.println("Reading is over!!!!"); } }
Output:-
Enter full path to the file(.txt):-D:/welcome.txt
Reading is over!!!!
Please keep tuned if you like this on my upcoming tutorial we will learn how could we create a simple GUI for this java app,which will consists of a JFileChooser ,a pause and resume button and a dropdown to select through the available voices.
Comments
Post a Comment