Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to implement a text speech to my code using Freetts; I\'ve already downlo

ID: 666593 • Letter: I

Question

I need to implement a text speech to my code using Freetts; I've already downloaded the jar files.

This is my code that I'm trying to implement a text speech to, its basically a jbutton when clicked on it shows a description of an animal, now i want to implement a text speech to it to be able to read the description.

First Jbutton is a Descrption of an Alligator

Second Jbutton is a Description Of a Bear

My code:

package Animals;

import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;

import javax.swing.JTextPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;

import java.io.*;
import com.sun.speech.freetts.*;

public class mainAnimals {

   private JFrame frame;

   /**
   * Launch the application.
   */
   public static void main(String[] args) {
       EventQueue.invokeLater(new Runnable() {
           public void run() {
               try {
                   mainAnimals window = new mainAnimals();
                   window.frame.setVisible(true);
               } catch (Exception e) {
                   e.printStackTrace();
               }
           }
       });
   }

   /**
   * Create the application.
   */
   public mainAnimals() {
       initialize();
   }

   /**
   * Initialize the contents of the frame.
   */
   private void initialize() {
       frame = new JFrame();
       frame.getContentPane().setBackground(new Color(112,183,140));
       frame.setBounds(100, 100,950, 850);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.getContentPane().setLayout(null);
       frame.setResizable(false);
      
       JButton btnNewButton = new JButton("");
       btnNewButton.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
           }
       });
       btnNewButton.setFocusPainted(false);
       btnNewButton.setContentAreaFilled(false);
       btnNewButton.setBorderPainted(false);
       btnNewButton.setBounds(705, 39, 170, 107);
       frame.getContentPane().add(btnNewButton);
      
      
      
      
       JButton btnAlligator = new JButton("");
       btnAlligator.setIcon(new javax.swing.ImageIcon(getClass().getResource("btnA.png")));
       btnAlligator.setBorderPainted(false);
       btnAlligator.setFocusPainted(false);
       btnAlligator.setContentAreaFilled(false);
       btnAlligator.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               btnNewButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("btnA1.jpg")));
               btnAlligator.setIcon(new javax.swing.ImageIcon(getClass().getResource("btnAa.jpg")));
               btnNewButton.setBounds(700, 50, 200, 150);
               JTextArea textArea = new JTextArea();
               textArea.setBackground(new Color(112,183,140));
               textArea.setBounds(660, 250, 301, 600);
               frame.getContentPane().add(textArea);
               textArea.setEditable(false);
               textArea.setVisible(true);
               textArea.setFont(new Font("Serif", Font.ITALIC, 22));
               textArea.setText("A is for Alligator: Type: Reptile Diet: Carnivore Average lifespan in the wild: 35 to 50 years Size: 10 to 15 ft (3 to 4.6 m) Weight: 1,000 lbs (453 kg) Group name: Congregation Did you know? The largest American alligator ever reported was supposedly 19.8 feet (6 meters) long, although there are doubts about the claim. Protection status: Recovered ");
           }
       });
       btnAlligator.setBounds(0, 0, 170, 107);
       frame.getContentPane().add(btnAlligator);
      
       JButton btnAnimal = new JButton("");
       btnAnimal.setIcon(new javax.swing.ImageIcon(getClass().getResource("btnB.png")));
       btnAnimal.setBorderPainted(false);
       btnAnimal.setFocusPainted(false);
       btnAnimal.setContentAreaFilled(false);
       btnAnimal.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               btnNewButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("btnB1.jpg")));
               btnNewButton.setBounds(700, 50, 200, 150);
               JTextArea textArea = new JTextArea();
               textArea.setBackground(new Color(112,183,140));
               textArea.setBounds(660, 250, 301, 600);
               frame.getContentPane().add(textArea);
               textArea.setEditable(false);
               textArea.setVisible(true);
               textArea.setFont(new Font("Serif", Font.ITALIC, 22));
               textArea.setText("Bear Type: MammalDiet: Omnivore Average lifespan in the wild: 25 years Size: 5 to 8 ft (1.5 to 2.5 m) Weight: 700 lbs (318 kg) Group name: Sloth or sleuth ");
             
               }
           });

This is the code I found online that can read text, but I didn't know how use it, program kept crashing:

import java.io.BufferedReader;  

import java.io.IOException;  

import java.io.InputStreamReader;  

import com.sun.speech.freetts.Voice;   

import com.sun.speech.freetts.VoiceManager;   

  

public class Demofreetts {  

// Default voice is Kevin16  

private static final String VOICENAME = "kevin16";  

  

public static void main(String[] args) {  

  Voice voice;  

  // Taking instance of voice from VoiceManager factory.  

  VoiceManager voiceManager = VoiceManager.getInstance();  

  voice = voiceManager.getVoice(VOICENAME);  

  // Allocating voice  

  voice.allocate();  

  // word per minute  

  voice.setRate(120);  

  voice.setPitch(100);  

  System.out.print("Enter your text: ");  

  // open up standard input  

  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  

  try {  

   // Ready to speak  

   voice.speak(br.readLine());  

  } catch (IOException e) {  

   // TODO Auto-generated catch block  

   e.printStackTrace();  

  }  

}  

}

Explanation / Answer

working java code

import javax.speech.*;  
import java.util.*;  
import javax.speech.synthesis.*;  

public class Text2Speech  
{  
String speaktext;
public void dospeak(String speak,String voicename)  
{  
    speaktext=speak;  
    String voiceName =voicename;  
    try  
    {  
        SynthesizerModeDesc desc = new SynthesizerModeDesc(null,"general", Locale.US,null,null);  
        Synthesizer synthesizer = Central.createSynthesizer(desc);  
        synthesizer.allocate();  
        synthesizer.resume();   
        desc = (SynthesizerModeDesc) synthesizer.getEngineModeDesc();   
        Voice[] voices = desc.getVoices();    
        Voice voice = null;
        for (int i = 0; i < voices.length; i++)  
        {  
            if (voices[i].getName().equals(voiceName))  
            {  
                voice = voices[i];  
                break;   
            }   
        }  
        synthesizer.getSynthesizerProperties().setVoice(voice);  
        System.out.print("Speaking : "+speaktext);  
        synthesizer.speakPlainText(speaktext, null);  
        synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);  
        synthesizer.deallocate();  
    }  
    catch (Exception e)
    {  
        String message = " missing speech.properties in " + System.getProperty("user.home") + " ";  
        System.out.println(""+e);  
        System.out.println(message);  
    }  
}  

public static void main(String[] args)  
{  
    Text2Speech obj=new Text2Speech(); obj.dospeak("Hello i am kevin ","kevin16");  
}  
}