?? playermodelimpl.java
字號:
* * @return the volume, or -1 if unknown, or an error occurred */ public float getVolume() { try { float adjustedVolume = synthesizer.getSynthesizerProperties().getVolume(); if (adjustedVolume < 0.5) { volume = 0; } else { volume = (float) ((adjustedVolume - 0.5) * 20); } } catch (Exception e) { e.printStackTrace(); } return volume; } /** * Sets the volume, in the range of 0 to 10. * * @param volume the new volume * * @return true if new volume is set; false otherwise */ public boolean setVolume(float volume) { try { float adjustedVolume = (float) (volume/20 + 0.5); if (synthesizer != null) { synthesizer.getSynthesizerProperties().setVolume (adjustedVolume); this.volume = volume; return true; } else { this.volume = volume; return false; } } catch (PropertyVetoException pve) { try { synthesizer.getSynthesizerProperties().setVolume(this.volume); } catch (PropertyVetoException pe) { pe.printStackTrace(); } return false; } } /** * Returns the speaking rate. * * @return the speaking rate, or -1 if unknown or an error occurred */ public float getSpeakingRate() { if (synthesizer != null) { return synthesizer.getSynthesizerProperties().getSpeakingRate(); } else { return -1; } } /** * Sets the speaking rate in terms of words per minute. * * @param wordsPerMin the new speaking rate * * @return the speaking rate, or -1 if unknown or an error occurred */ public boolean setSpeakingRate(float wordsPerMin) { float oldSpeed = getSpeakingRate(); SynthesizerProperties properties = synthesizer.getSynthesizerProperties(); try { properties.setSpeakingRate(wordsPerMin); return true; } catch (PropertyVetoException pve) { try { properties.setSpeakingRate(oldSpeed); } catch (PropertyVetoException pe) { pe.printStackTrace(); } return false; } } /** * Returns the baseline pitch for the current synthesis voice. * * @return the baseline pitch for the current synthesis voice */ public float getPitch() { return synthesizer.getSynthesizerProperties().getPitch(); } /** * Sets the baseline pitch for the current synthesis voice. * * @param pitch the baseline pitch * * @return true if new pitch is set; false otherwise */ public boolean setPitch(float pitch) { float oldPitch = getPitch(); try { synthesizer.getSynthesizerProperties().setPitch(pitch); return true; } catch (PropertyVetoException pve) { try { synthesizer.getSynthesizerProperties().setPitch(oldPitch); } catch (PropertyVetoException pe) { pe.printStackTrace(); } return false; } } /** * Returns the pitch range for the current synthesis voice. * * @return the pitch range for the current synthesis voice */ public float getRange() { return synthesizer.getSynthesizerProperties().getPitchRange(); } /** * Sets the pitch range for the current synthesis voice. * * @param range the pitch range * * @return true if new range is set; false otherwise */ public boolean setRange(float range) { float oldRange = getRange(); try { synthesizer.getSynthesizerProperties().setPitchRange(range); return true; } catch (PropertyVetoException pve) { try { synthesizer.getSynthesizerProperties().setPitchRange(oldRange); } catch (PropertyVetoException pe) { pe.printStackTrace(); } return false; } } /** * Sets the list of voices using the given Synthesizer mode description. * * @param modeDesc the synthesizer mode description */ public void setVoiceList(SynthesizerModeDesc modeDesc) { Voice[] voices = modeDesc.getVoices(); voiceList.removeAllElements(); for (int i = 0; i < voices.length; i++) { voiceList.addElement(new MyVoice(voices[i].getName(), voices[i].getGender(), voices[i].getAge(), voices[i].getStyle())); } } /** * Returns the play list. * * @return the play list */ public ListModel getPlayList() { return playList; } /** * Returns the list of voices. * * @return the list of voices */ public ListModel getVoiceList() { return voiceList; } /** * Returns the list synthesizers * * @return the synthesizer list */ public ListModel getSynthesizerList() { return synthesizerList; } /** * Returns the Playable object at the given index of the play list. * * @return the Playable object */ public Object getPlayableAt(int index) { return null; } /** * Adds the given Playable object to the end of the play list. * * @param playable the Playable object to add */ public void addPlayable(Playable playable) { playList.addElement(playable); } /** * Removes the playable at the given position from the list * * @param index the index of the Playable to remove */ public void removePlayableAt(int index) { if (index < playList.getSize()) { playList.removeElementAt(index); } } /** * Prints debug statements. * * @param statement debug statements */ public static void debugPrint(String statement) { if (debug) { System.out.println(statement); } }}/** * A Voice that implements the <code>toString()</code> method so that * it returns the name of the person who owns this Voice. */class MyVoice extends Voice { /** * Constructor provided with voice name, gender, age and style. * * @param name the name of the person who owns this Voice * @param gender the gender of the person * @param age the age of the person * @param style the style of the person */ public MyVoice(String name, int gender, int age, String style) { super(name, gender, age, style); } /** * Returns the name of the person who owns this Voice. * * @param String the name of the person */ public String toString() { return getName(); }}/** * A SynthesizerModeDesc that implements the <code>toString()</code> * method so that it returns the name of the synthesizer. */class MySynthesizerModeDesc extends SynthesizerModeDesc { private PlayerModel playerModel = null; private Synthesizer synthesizer = null; private Monitor monitor = null; private boolean synthesizerLoaded = false; /** * Constructs a MySynthesizerModeDesc with the attributes from * the given SynthesizerModeDesc. * * @param modeDesc the SynthesizerModeDesc to get attributes from */ public MySynthesizerModeDesc(SynthesizerModeDesc modeDesc, PlayerModel playerModel) { super(modeDesc.getEngineName(), modeDesc.getModeName(), modeDesc.getLocale(), modeDesc.getRunning(), modeDesc.getVoices()); this.playerModel = playerModel; } /** * Returns true if the synthesizer is already loaded. * * @return true if the synthesizer is already loaded */ public synchronized boolean isSynthesizerLoaded() { if (synthesizer == null) { return false; } return ((synthesizer.getEngineState() & Engine.ALLOCATED) != 0); } /** * Returns a Synthesizer that fits the description of this * MySynthesizerModeDesc. If the synthesize was never loaded, * it is loaded in a separate thread. * * @return a Synthesizer */ public synchronized Synthesizer getSynthesizer() { debugPrint("MyModeDesc.getSynthesizer(): " + getEngineName()); return synthesizer; } /** * Creates the Synthesizer and its Monitor. * * @return the created Synthesizer */ public Synthesizer createSynthesizer() { try { debugPrint("Creating " + getEngineName() + "..."); synthesizer = Central.createSynthesizer(this); if (synthesizer == null) { System.out.println("Central created null synthesizer"); } else { synthesizer.allocate(); synthesizer.resume(); monitor = new Monitor(synthesizer, getEngineName()); debugPrint("...created monitor"); } } catch (Exception e) { e.printStackTrace(); } return synthesizer; } /** * Allocates the synthesizer if it has never been allocated. This * method should be called after method <code>createSynthesizer()</code>. * It spawns a new thread to allocate the synthesizer. */ public Synthesizer loadSynthesizer() { try { if (!synthesizerLoaded) { debugPrint("Loading " + getEngineName() + "..."); synthesizerLoaded = true; SynthesizerLoader loader = new SynthesizerLoader loader.start(); } } catch (Exception e) { e.printStackTrace(); } return synthesizer; } /** * Returns the Monitor of this Synthesizer. * * @return the Monitor */ public synchronized Monitor getMonitor() { if (monitor == null) { createSynthesizer(); } return monitor; } /** * Returns the PlayerModel. * * @return the PlayerModel */ public PlayerModel getPlayerModel() { return playerModel; } /** * Returns the name of the Synthesizer. * * @return the name of the Synthesizer */ public String toString() { return getEngineName(); } /** * Prints debug statements. * * @param statement debug statements */ private void debugPrint(String statement) { PlayerModelImpl.debugPrint(statement); }}/** * A Thread that loads the Synthesizer. */class SynthesizerLoader extends Thread { private Synthesizer synthesizer; private MySynthesizerModeDesc modeDesc; private PlayerModel playerModel; /** * Constructs a SynthesizerLoaded which loads the given Synthesizer. * * @param synthesizer the Synthesizer to load * @param modeDesc the MySynthesizerModeDesc from which we can retrieve * the PlayerModel */ public SynthesizerLoader(Synthesizer synthesizer, MySynthesizerModeDesc modeDesc) { this.synthesizer = synthesizer; this.modeDesc = modeDesc; this.playerModel = modeDesc.getPlayerModel(); } /** * Implements the <code>run()</code> method of the Thread class. */ public void run() { try { System.out.println("allocating..."); synthesizer.allocate(); System.out.println("...allocated"); synthesizer.resume(); System.out.println("...resume"); playerModel.setVoiceList(modeDesc); } catch (Exception e) { e.printStackTrace(); } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -