Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to work out how to implement text to speech with java 11 and have written some classes in order to read a text ("Dracula") and then and option to speak the text. Classes: 


package draculatext;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Start {
	public static Queue<String> wholeText = new LinkedList<String>();
	public static BookReadToScreen bookReadToScreen = new BookReadToScreen();
	public static BookReadSpeech bookReadSpeech = new BookReadSpeech();

	public static void main(String[] args) {
		System.out.println("Welcome to QUB Book Read");
		showMenu();
		System.out.println("Application Done");
	}

	public static void showMenu() {
		System.out.println();
		Scanner scanner = new Scanner(System.in);
		int option;
		System.out.println("Book reading ");
		do {
			System.out.println("1. Load book");
			System.out.println("2. Read / Resume book");
			System.out.println("3. Pause read");
			System.out.println("4. Speak book");
			System.out.println("5. Pause speak");
			System.out.println("6. Quit");
			System.out.println("Enter option ...");
			option = scanner.nextInt();
			switch (option) {
			case 1:
				readBookFile();
				System.out.println("Book loaded");
				break;
			case 2:
				if (wholeText.isEmpty()) {
					System.out.println("Load book first...");
				} else {
					readBookToScreen();
				}
				break;
			case 3:
				stopRead();
				break;
			case 4:
				speakBook();
				break;
			case 5:
				stopSpeech();
				break;
			case 6:
// ensuring all threads are going finish
				bookReadToScreen.setStopRead(true);
				bookReadSpeech.setStopSpeech(true);
				System.out.println("Quitting...");
				break;
			default:
				System.out.println("Try options again...");
			}
		} while (option != 6);
		scanner.close();
	}

	public static void readBookFile() {
		try {
			FileReader fr = new FileReader(new File("Dracula.txt"));
			BufferedReader br = new BufferedReader(fr);
// read the first line.
			String line = br.readLine();
			while (line != null) {
				wholeText.add(line);
				line = br.readLine();
			}
			fr.close();
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void readBookToScreen() {
		bookReadToScreen.setStopRead(false);
		Thread readToScreenThread = new Thread(bookReadToScreen);
		readToScreenThread.start();
	}

	public static void stopRead() {
		System.out.println("Going to stop the read");
		bookReadToScreen.setStopRead(true);
	}

	public static void speakBook() {
		bookReadSpeech.setStopSpeech(false);
		Thread speakTheBook = new Thread(bookReadSpeech);
		speakTheBook.start();
	}

	public static void stopSpeech() {
		System.out.println("Going stop speech");
		bookReadSpeech.setStopSpeech(true);
	}
}


package draculatext;

public class BookReadToScreen implements Runnable {
	private boolean stopRead = false;

	@Override
	public void run() {
		displayTextLineByLine();
	}

	private void displayTextLineByLine() {
		while (!Start.wholeText.isEmpty()) {
			System.out.println(Start.wholeText.remove());
// check if need to stop
			if (stopRead) {
				System.out.println("Stopping read to screen ..");
				return;
			}
			try {
				Thread.currentThread().sleep(1000);
			} catch (InterruptedException e) {
// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * @param stopRead the stopRead to set
	 */
	public void setStopRead(boolean stopRead) {
		this.stopRead = stopRead;
	}
}


package draculatext;

import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;

public class BookReadSpeech implements Runnable {
	private boolean stopSpeech = false;

	@Override
	public void run() {
		Voice voice;// Creating object of Voice class
		voice = VoiceManager.getInstance().getVoice("kevin");// Getting voice
		if (voice != null) {
			voice.allocate();// Allocating Voice
		}
		try {
			voice.setRate(140);// Setting the rate of the voice
			voice.setPitch(70);// Setting the Pitch of the voice
			voice.setVolume(1);// Setting the volume of the voice
			while (!Start.wholeText.isEmpty()) {
				voice.speak(Start.wholeText.remove());
				if (stopSpeech) {
					System.out.println("Going stop speaking");
					return;
				}
			}
		} catch (Exception e) {
		}
	}

	/**
	 * @param stopSpeech the stopSpeech to set
	 */
	public void setStopSpeech(boolean stopSpeech) {
		this.stopSpeech = stopSpeech;
	}
}


 think all the code is fine and I have added freets.jar and freets-jsapi10.jar to my build path, but everytime i attempt to run the text to speech an error NoClassDefFoundError is thrown. console with error: https://i.stack.imgur.com/Ruhtc.png

Any help would be much appreciated. Thanks :)


What I have tried:

I have tried changing my execution environment to earlier versions of java
Posted
Updated 13-Mar-22 22:44pm

1 solution

The error message is telling you that the system cannot find the VoiceManager, which suggests that it is not aware of the location of the jar file(s). Make sure that the file's location is passed to the virtual machine by adding it as follows:
java -Djava.library.path="C:\MyFolder\libs" BookReadSpeech
 
Share this answer
 
Comments
Cameron Grant 2021 14-Mar-22 5:38am    
Hi Richard. Thank you very much for your reply and help. I think I have passed the file's location to the virtual machine correctly, but I am getting the same error message. Screenshots of refernced library: https://i.stack.imgur.com/qOwd2.png
Screenshot ofimports: https://i.stack.imgur.com/3E6b8.png
Richard MacCutchan 14-Mar-22 5:48am    
Have you done what I suggested in my Solution above? The import statements at build time have no bearing on where the jar files are at run time.
Cameron Grant 2021 14-Mar-22 6:14am    
Hi Richard. Sorry, so where would I add that to pass on the files location to the virtual machine? In the java build path it suggests it know the files location. Sorry, I'm quite new to programming so just trying to get my head round this :/
Richard MacCutchan 14-Mar-22 6:25am    
I gave you the detail in my solution above, it is just an added option when you run the program.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900