FileDocCategorySizeDatePackage
SpeechListener.javaAPI DocExample5313Wed Jul 17 00:42:38 BST 2002com.wiverson.macosbook.speech

SpeechListener

public class SpeechListener extends JDialog implements ActionListener, com.apple.speech.recognition.UnrecognizedEventListener, com.apple.speech.recognition.DetectedEventListener, com.apple.speech.recognition.DoneEventListener

Fields Summary
static com.apple.speech.recognition.Recognizer
mySpeechRecognizer
static com.apple.speech.recognition.LanguageModel
myLanguageModel
static com.apple.speech.synthesis.Synthesizer
mySynthesizer
private JLabel
statusLabel
private JComboBox
manualCommandMenu
private String
computerName
private static final int
DAY
private static final int
SONG
private static final int
QUIT
private static final int
BEEP
private String[]
tasks
Constructors Summary
public SpeechListener()

    
    
     
    
        this.getContentPane().setLayout(new BorderLayout());
        statusLabel = new JLabel("Ready.");
        statusLabel.setHorizontalTextPosition(statusLabel.LEFT);
        this.getContentPane().add(statusLabel, BorderLayout.CENTER);
        
        manualCommandMenu = new JComboBox();
        manualCommandMenu.setModel(new javax.swing.DefaultComboBoxModel(tasks));
        manualCommandMenu.addActionListener(this);
        
        this.getContentPane().add(manualCommandMenu, BorderLayout.EAST);
        
        this.pack();
        this.setSize(300, 50);
        this.setTitle("Address me as " + computerName);
        
        // Set up to talk have the computer talk back.
        if(mySynthesizer == null)
            mySynthesizer = new com.apple.speech.synthesis.Synthesizer();
        
        try
        {
            // Hack for workaround of bug which
            // prevents Java apps from receiving
            // AppleEvents in Mac OS X 10.0
            com.apple.ae.AppleEventFunctions.initAE();
            
            // Create the SpeechRecoginizer.  Speech is activated lazily upon startup.
            mySpeechRecognizer = new com.apple.speech.recognition.Recognizer();
            
            // Create & setup the LanguageModel which we will add our phrases to.
            myLanguageModel = new com.apple.speech.recognition.LanguageModel();
            mySpeechRecognizer.setLanguageModel(myLanguageModel);
            
            // Add the phrases we are looking for.
            // Note that we need to add the computer's address first.
            // Still, easier than using the more complex API
            String[] full_tasks = new String[tasks.length];
            for(int i = 0; i < tasks.length; i++)
                full_tasks[i] = computerName + tasks[i];
            
            myLanguageModel.setPhrases(full_tasks);
            
            // Start the recoginizer
            mySpeechRecognizer.start();
            
            // Listen for speech events
            mySpeechRecognizer.addDoneEventListener(this);
            mySpeechRecognizer.addUnrecognizedEventListener(this);
            mySpeechRecognizer.addDetectedEventListener(this);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent actionEvent)

        if(actionEvent.getSource() instanceof JComboBox)
        {
            doCommand(((JComboBox)actionEvent.getSource()).getSelectedItem().toString());
        }
    
public voiddoCommand(java.lang.String input)

        statusLabel.setText("I heard " + input);
        
        if(input.compareTo(tasks[DAY]) == 0)
        {
            mySynthesizer.speakText(new java.util.Date().toString());
        }
        
        if(input.compareTo(tasks[SONG]) == 0)
        {
            mySynthesizer.speakText("Sorry, I'm shy");
        }
        
        if(input.compareTo(tasks[QUIT]) == 0)
        {
            System.exit(0);
        }
        
        if(input.compareTo(tasks[BEEP]) == 0)
        {
            java.awt.Toolkit.getDefaultToolkit().beep();
        }
    
public voidhandleDetectedEvent(com.apple.speech.recognition.DetectedEvent detectedEvent)

        statusLabel.setText("Listening...");
    
public voidhandleDoneEvent(com.apple.speech.recognition.DoneEvent doneEvent)

        String command = doneEvent.getPhraseRecognized();
        if(command != null)
        {
            command = command.substring(computerName.length(), command.length() );
            doCommand(command);
        } else
        {
            statusLabel.setText("Can't understand...?");
        }
    
public voidhandleUnrecognizedEvent(com.apple.speech.recognition.UnrecognizedEvent unrecognizedEvent)

        statusLabel.setText("Unrecognized...");
    
static voidmain(java.lang.String[] args)

    
       
    
        (new SpeechListener()).show();