?? playermodelimpl.java
字號:
/** * Copyright 2001 Sun Microsystems, Inc. * * See the file "license.terms" for information on usage and * redistribution of this file, and for a DISCLAIMER OF ALL * WARRANTIES. */import java.beans.PropertyVetoException;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;import java.util.Enumeration;import java.util.Locale;import java.util.HashSet;import java.util.Set;import java.util.Iterator;import javax.speech.AudioException;import javax.speech.Central;import javax.speech.Engine;import javax.speech.EngineException;import javax.speech.EngineList;import javax.speech.synthesis.Synthesizer;import javax.speech.synthesis.SynthesizerModeDesc;import javax.speech.synthesis.SynthesizerProperties;import javax.speech.synthesis.Voice;import javax.swing.DefaultComboBoxModel;import javax.swing.DefaultListModel;import javax.swing.JFrame;import javax.swing.ListModel;import com.sun.speech.engine.synthesis.SynthesizerMonitor;/** * Implements the text-to-speech data model of the Player application, using * JSAPI. It should work with any JSAPI implementation. */public class PlayerModelImpl implements PlayerModel { private Synthesizer synthesizer; private Monitor monitor; private boolean monitorVisible = false; private boolean paused = false; private boolean stopped = false; private boolean playingFile = false; private DefaultListModel playList; private DefaultComboBoxModel synthesizerList; private DefaultComboBoxModel voiceList; private float volume = -1; private static boolean debug = false; private Set loadedSynthesizers; /** * Constructs a default PlayerModelImpl. */ public PlayerModelImpl() { playList = new DefaultListModel(); synthesizerList = new DefaultComboBoxModel(); voiceList = new DefaultComboBoxModel(); loadedSynthesizers = new HashSet(); } /** * Creates a FreeTTS synthesizer. */ public void createSynthesizers() { try { EngineList list = Central.availableSynthesizers(null); Enumeration e = list.elements(); while (e.hasMoreElements()) { MySynthesizerModeDesc myModeDesc = new MySynthesizerModeDesc ((SynthesizerModeDesc) e.nextElement(), this); debugPrint(myModeDesc.getEngineName() + " " + myModeDesc.getLocale() + " " + myModeDesc.getModeName() + " " + myModeDesc.getRunning()); synthesizerList.addElement(myModeDesc); } if (synthesizerList.getSize() > 0) { setSynthesizer(0); } else { System.err.println(noSynthesizerMessage()); } if (synthesizer == null) { System.err.println("PlayerModelImpl: Can't find synthesizer"); System.exit(1); } } catch (Exception e) { e.printStackTrace(); } } /** * Returns a "no synthesizer" message, and asks * the user to check if the "speech.properties" file is * at <code>user.home</code> or <code>java.home/lib</code>. * * @return a no synthesizer message */ static private String noSynthesizerMessage() { String message = "No synthesizer created. This may be the result of any\n" + "number of problems. It's typically due to a missing\n" + "\"speech.properties\" file that should be at either of\n" + "these locations: \n\n"; message += "user.home : " + System.getProperty("user.home") + "\n"; message += "java.home/lib: " + System.getProperty("java.home") + File.separator + "lib\n\n" + "Another cause of this problem might be corrupt or missing\n" + "voice jar files in the freetts lib directory. This problem\n" + "also sometimes arises when the freetts.jar file is corrupt\n" + "or missing. Sorry about that. Please check for these\n" + "various conditions and then try again.\n"; return message; } /** * Performs TTS on the given Playable. * * @param playable the Playable object to play */ public void play(Playable playable) { if (playable != null) { if (playable.getType() == PlayableType.TEXT) { play(playable.getText()); } else if (playable.getType() == PlayableType.JSML) { playJSML(playable.getText()); } else if (playable.getType() == PlayableType.TEXT_FILE || playable.getType() == PlayableType.JSML_FILE) { playFile(playable.getFile(), playable.getType()); } else if (playable.getType() == PlayableType.URL) { try { playURL(new URL(playable.getName())); } catch (MalformedURLException mue) { mue.printStackTrace(); } } } } /** * Performs TTS on the object at the given index of the play list. * * @param index index of the object in the playlist to play */ public void play(int index) { if (0 <= index && index < playList.getSize()) { Playable playable = (Playable) playList.getElementAt(index); if (playable != null) { play(playable); } } } /** * Performs text-to-speech on the given text. * * @param text the text to perform TTS */ private void play(String text) { synthesizer.speakPlainText(text, null); } /** * Performs text-to-speech on the given JSML text. * * @param text the text to perform TTS */ private void playJSML(String jsmlText) { try { synthesizer.speak(jsmlText, null); } catch (Exception e) { e.printStackTrace(); } } /** * Plays the text in the given File. * * @param file the File to play * @param type the file type */ private void playFile(File file, PlayableType type) { try { FileInputStream fileStream = new FileInputStream(file); playInputStream(fileStream, type); } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(); } } /** * Plays the text in the given File. * * @param file the File to play * @param type the file type */ private void playInputStream(InputStream inStream, PlayableType type) { playingFile = true; if (inStream != null) { try { BufferedReader reader = new BufferedReader (new InputStreamReader(inStream)); String line = ""; if (type == PlayableType.TEXT_FILE) { while (!isStopped() && (line = reader.readLine()) != null) { if (line.length() > 0) { play(line); } } } else if (type == PlayableType.JSML_FILE) { String fileText = ""; while ((line = reader.readLine()) != null) { fileText += line; } if (fileText != null && fileText.length() > 0) { playJSML(fileText); } } stopped = false; } catch (IOException ioe) { ioe.printStackTrace(); } } playingFile = false; } /** * Plays the contents of the given URL. * * @param url the URL to play */ private void playURL(URL url) { try { synthesizer.speak(url, null); } catch (Exception e) { e.printStackTrace(); } } /** * Returns true if the player is paused. * * @return true if the player is paused, false otherwise */ public synchronized boolean isPaused() { return paused; } /** * Pauses the player. */ public synchronized void pause() { paused = true; synthesizer.pause(); } /** * Resumes the player. */ public synchronized void resume() { paused = false; try { synthesizer.resume(); } catch (AudioException ae) { ae.printStackTrace(); } } /** * Stops the player if it is playing. */ public synchronized void stop() { if (playingFile) { stopped = true; } synthesizer.cancelAll(); } /** * Cancels the currently playing item. */ public void cancel() { synthesizer.cancel(); } /** * Close this playable */ public void close() { for (Iterator i = loadedSynthesizers.iterator(); i.hasNext();) { Synthesizer synth = (Synthesizer) i.next(); try { synth.deallocate(); } catch (EngineException ee) { System.out.println("Trouble closing the synthesizer: " + ee); } } } /** * Returns true if the Player is currently being stopped. * * @return true if the Player is currently being stopped; false otherwise */ private synchronized boolean isStopped() { return stopped; } /** * Sets whether the Monitor is visible * * @param visible true to set the Monitor as visible */ public void setMonitorVisible(boolean visible) { monitorVisible = visible; if (monitor != null) { monitor.setVisible(monitorVisible); } } /** * Tells whether the monitor is visible. * * @return true if the monitor is visible, false otherwise */ public boolean isMonitorVisible() { return monitorVisible; } /** * Returns the monitor of the synthesizer at the given index. * * @param index the position of the synthesizer in the synthesizer list * * @return the monitor of the specified synthesizer */ public Monitor getMonitor(int index) { MySynthesizerModeDesc myModeDesc = (MySynthesizerModeDesc) synthesizerList.getElementAt(index); Monitor monitor = null; if (myModeDesc != null) { monitor = myModeDesc.getMonitor(); } return monitor; } /** * Returns the monitor of the current synthesizer. * * @return the monitor of the current synthesizer */ public Monitor getMonitor() { return monitor; } /** * Sets the current monitor. * * @param monitor the current monitor */ public void setMonitor(Monitor monitor) { this.monitor = monitor; } /** * Sets the Synthesizer at the given index to use * * @param index index of the synthesizer in the list */ public void setSynthesizer(int index) { MySynthesizerModeDesc myModeDesc = (MySynthesizerModeDesc) synthesizerList.getElementAt(index); if (myModeDesc != null) { if (isMonitorVisible()) { if (monitor != null) { monitor.setVisible(false); } } synthesizer = myModeDesc.getSynthesizer(); if (synthesizer == null) { synthesizer = myModeDesc.createSynthesizer(); if (synthesizer == null) { debugPrint("still null"); } else { debugPrint("created"); } } else { debugPrint("not null"); } monitor = myModeDesc.getMonitor(); if (myModeDesc.isSynthesizerLoaded()) { setVoiceList(myModeDesc); } else { myModeDesc.loadSynthesizer(); } loadedSynthesizers.add(synthesizer); synthesizerList.setSelectedItem(myModeDesc); } } /** * Sets the Voice at the given to use. * * @param index the index of the voice */ public void setVoice(int index) { try { Voice voice = (Voice) voiceList.getElementAt(index); if (voice != null) { float oldVolume = getVolume(); float oldSpeakingRate = getSpeakingRate(); synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY); synthesizer.getSynthesizerProperties().setVoice(voice); setVolume(oldVolume); setSpeakingRate(oldSpeakingRate); voiceList.setSelectedItem(voice); } } catch (PropertyVetoException pve) { pve.printStackTrace(); } catch (InterruptedException ie) { ie.printStackTrace(); } } /** * Returns the volume, in the range of 0 to 10.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -