Creating the Help Interface


The SpeakHelpText.java file converts the text attached to the menu options and buttons to speech. This file reads out help information in the voice selected by an end user .

Listing 4-3 shows the contents of the SpeakHelpText.java file:

Listing 4-3: The SpeakHelpText.java File
start example
 /*Import required io classes.*/ import java.io.File; /*Import required util classes.*/ import java.util.Locale; import java.util.Vector; import javax.speech.synthesis.SynthesizerAdapter; import javax.speech.synthesis.SynthesizerEvent; /*Import required speech classes.*/ import javax.speech.Central; import javax.speech.Engine; import javax.speech.EngineList; import javax.speech.synthesis.Synthesizer; import javax.speech.synthesis.SynthesizerModeDesc; import javax.speech.synthesis.SynthesizerProperties; import javax.speech.synthesis.Voice; /* Class:SpeakHelpText-Speaks the text Fields: voiceName: Represents the voice selected by the end user. synthesizer: Represents an object of Synthesizer class. Methods: speakSelText(): Uses the object of Synthesizer class to speak the text. closeSynthesizer(): Closes the synthesizer and stops the speaking operation. pauseSynthesizer(): Pauses the synthesizer. resumeSynthesizer(): Resumes the paused speaking operation of the synthesizer. */ public class SpeakHelpText extends SynthesizerAdapter {    /*Declare variables.*/    String l;    String voiceName;    SynthesizerModeDesc desc;    Synthesizer synthesizer;    SpeakHelp txtpad;    /*    SpeakHelpText():    Parameters:    k: String representing the text that the synthesizer speaks.    selvoice: String representing the voice selected by the end user.    Return Type: NA    */    public SpeakHelpText(String k, String selvoice)     {       /*       Set the value of l to the text that the synthesizer speaks.       */       l=k;       /*       Set the value of voiceName to the voice selected by the user.       */       txtpad=new SpeakHelp();       voiceName = selvoice;       System.out.println("Using voice: " + voiceName);       try        {          /*          Initialize an object of SynthesizerModeDesc class.          */          desc = new SynthesizerModeDesc          (             null,                       "general",                  Locale.US,                  null,                       null);                      synthesizer = Central.createSynthesizer(desc);             /*             Prints the message if no synthesizer is available.             */             if (synthesizer == null)              {                String message = "\nCan't find synthesizer.\n"                + "Make sure that there is a \"speech.properties\" file "                + "at either of these locations: \n";                message += "user.home:"                + System.getProperty("user.home") + "\n";                message += "java.home/lib: " + System.getProperty("java.home")                + File.separator + "lib\n";                System.err.println(message);                System.exit(1);             }             /* Allocate a synthesizer to speak text.*/             synthesizer.allocate();             synthesizer.resume();             desc = (SynthesizerModeDesc)             synthesizer.getEngineModeDesc();             /*             Retrieve all the available voices in an array.             */             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;             }          }          /*          Print error message if selected voice is not available.          */          if (voice == null)           {             System.err.println("Synthesizer does not have a voice named "             + voiceName + ".");             System.exit(1);          }          /*          Set the selected voice for the synthesizer.          */          Synthesizer.getSynthesizerProperties().setVoice(voice);       }       catch(Exception e)       {          System.out.println(e);       }    }    /*    speakSelText(): Speaks the text    Parameters: NA    Return Value: NA    */    public void speakSelText(SpeakHelp st)    {       try       {          txtpad=st;          /*          Invoke speakPlainText() method to speak the text.          */          synthesizer.cancelAll();          synthesizer.speakPlainText(l, null);          System.out.println("Speaking Text");       }       catch (Exception e)        {          e.printStackTrace();       }       try       {          /*          Invoke waitEngineState() method to wait end of speech.          */          synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);          txtpad.stopText.setEnabled(false);          txtpad.speakText.setEnabled(true);          txtpad.pause.setEnabled(false);          txtpad.resume.setEnabled(false);          txtpad.stop.setEnabled(false);       }       catch(Exception e)       {       }    }    /*    closeSynthesizer(): Close the synthesizer and stop the speaking operation.    Parameters: NA    Return Type: NA    */    public void closeSynthesizer()    {       try       {           /*          Stop the synthesizer and cancel the speaking operation.          */          synthesizer.cancelAll();          System.out.println("Speaking Operation Cancelled");       }       catch (Exception e)        {          e.printStackTrace();       }     }    /*    pauseSynthesizer(): Pause the synthesizer. This pauses the text that the synthesizer is currently    speaking.    Parameters: NA    Return Value: NA    */    public void pauseSynthesizer()    {       try       {          /*Pause the synthesizer.*/          synthesizer.pause();          System.out.println("Speaking Operation Paused");       }       catch(Exception e)       {          e.printStackTrace();       }    }    /*    resumeSynthesizer(): Resumes the speaking operation that is paused by the end user.    Parameters: NA    Return Value: NA    */    public void resumeSynthesizer()    {       try       {          /*          Resume the synthesizer that speaks the text.           */          synthesizer.resume();          System.out.println("Speaking Operation Resumed");       }       catch(Exception e)       {          e.printStackTrace();       }    } } 
end example
 

Download this listing .

In the above code, the constructor of the SpeakHelpText class takes two strings as input parameters. The k string represents the text attached to the menu options that the synthesizer reads out to provide help. The selvoice string represents the voice that an end user selects to read out the text.

The methods defined in Listing 4-3 are:

  • speakSelText (): Uses the object of the Synthesizer class to convert text to speech.

  • closeSynthesizer (): Closes the synthesizer and stops the audio operation.

  • pauseSynthesizer (): Pauses the synthesizer.

  • resumeSynthesizer (): Resumes the paused audio operation of the synthesizer.

Click the Help button in the user interface to listen to the help information for the selected menu. The question mark icon appears, as shown in Figure 4-6:

click to expand: this figure shows the interface of the voice help application with the help icon selected.
Figure 4-6: The Help Interface



Java InstantCode. Developing Applications using Java Speech API
Java InstantCode. Developing Applications using Java Speech API
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 46

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net