⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simpleplayergui.java

📁 j2me手机上的短信开发实例
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* * @(#)SimplePlayerGUI.java	1.10 04/08/10 * * Copyright (c) 2000-2004 Sun Microsystems, Inc. All rights reserved. * PROPRIETARY/CONFIDENTIAL * Use is subject to license terms */package example.mmademo;import java.util.*;import java.io.*;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.media.*;import javax.microedition.media.control.*;/** * GUI functionality for SimplePlayer. * This class is used by SimplePlayerCanvas and SimplePlayerForm and * provides the actual logic. * SimplePlayerCanvas and SimplePlayerForm provide the interaction of * the main window with the user. * * @version 1.10 */public class SimplePlayerGUI implements CommandListener, PlayerListener,					ItemStateListener {    // hack to use the form-based player for video    // if you set this to false, the currently active player is used    private final static boolean USE_FORM_PLAYER_FOR_VIDEO = false;    // maximum rate to support, in percent    private final static int LIMIT_RATE = 300;    // loop    private static final int LOOP_ONCE = 0;    private static final int LOOP_3    = 1;    private static final int LOOP_INF  = 2;    private int loopmode = LOOP_ONCE;    // timer (for time display and karaoke asynchronous event)    private static final int DEFAULT_TIMER_INTERVAL = 50;    private int timerInterval = DEFAULT_TIMER_INTERVAL;    // gauge    private static final int GAUGE_NONE   = -1;    private static final int GAUGE_VOLUME = 0;  // gauge is used as volume slider    private static final int GAUGE_RATE   = 1;  // gauge is used as rate slider    private static final int GAUGE_TEMPO  = 2;  // gauge is used as tempo slider    private static final int GAUGE_PITCH  = 3;  // gauge is used as pitch slider    private int gaugeMode = GAUGE_NONE;    private Gauge gauge = null;    private StringItem gaugeLabel = null;    private Form gaugeForm = null;    // display timer    private Timer guiTimer=null;    private TimerTask timeDisplayTask=null;    // Commands    Command backCommand = new Command("Back", Command.BACK, 1); // is used in SimplePlayerCanvas    private Command playCommand = new Command("Play", Command.ITEM, 1);    private Command stopCommand = new Command("Stop", Command.ITEM, 1);    private Command metaCommand = new Command("META data", Command.ITEM, 3);    private Command volCommand = new Command("Volume", Command.ITEM, 2);    private Command muteCommand = new Command("Mute", Command.ITEM, 1);    private Command unmuteCommand = new Command("Unmute", Command.ITEM, 1);    private Command loopCommand = new Command("Loopmode", Command.ITEM, 4);    private Command stcCommand = new Command("Stop in 5 sec", Command.ITEM, 4);    private Command selectCommand = new Command("Select", Command.ITEM, 1);    private Command skipFwCommand = new Command("Skip Forward", Command.ITEM, 5);    private Command skipBwCommand = new Command("Skip Backward", Command.ITEM, 5);    private Command rewindCommand = new Command("Rewind", Command.ITEM, 5);    private Command rateCommand = new Command("Rate", Command.ITEM, 5);    private Command tempoCommand = new Command("Tempo", Command.ITEM, 5);    private Command pitchCommand = new Command("Pitch", Command.ITEM, 5);    private Command fullScreenCommand = new Command("Full Screen: ON", Command.ITEM, 5);    private Command normalScreenCommand = new Command("Full Screen: OFF", Command.ITEM, 5);    private Command startRecordCommand = new Command("Start Recording", Command.ITEM, 5);    private Command stopRecordCommand = new Command("Stop Recording", Command.ITEM, 5);    private Command helpCommand = new Command("Quick Help", Command.ITEM, 10);    // display    private Parent parent;    private Displayable display;    private static Image logo = null;    // full screen    private boolean fullScreen = false;    // recording    private boolean isRecording = false;    private String recordLocator = "file:///root1/recording.wav";    // the player instance    private Player player=null;    // song descriptors    private String title;    private InputStream songInputStream;    private String durationStr;    private String songContentType="";    private String songLocator="";    private String songName="";    private String[] songDisplayNames = new String[0];    private int currSongDisplay = 0;    private int changeSongDisplayCounter = 0;    private static final int SONG_DISPLAY_COUNTER = 2000 / DEFAULT_TIMER_INTERVAL; // 2 seconds    // karaoke support    private final static int LYRICS_EVENT = 0x60;       // not an official event    private final static String LYRICS_KEY = "lyrics";  // not an official key for MetaDataControl    // indexes in params array returned by getKaraokeStr()    public final static int KARAOKE_LINE_COUNT = 0;    public final static int KARAOKE_LINE = 1;    public final static int KARAOKE_LINE_INDEX = 2;    public final static int KARAOKE_SYLLABLE_LENGTH = 3;    private boolean karaokeMode = false;    private String karaokeLyrics = "";    private int currKaraokeLine = 0;    private int currKaraokeLinePos = 0;    private int currKaraokeLength = 0;    private String[] karaokeLines = null;    private int[] karaokeLinePos = null;  // start position in karaokeLyrics for each line    private int karaokeLineCount = 0;    private int redisplayKaraokeCounter = 0; // asynchronous switch to next Karaoke page    private int nextKaraokePos = 0; // for asynchronous display of next line/phrase    // pause/resume support    private boolean restartOnResume = false;    private static void debugOut(String s) {	Utils.debugOut("SimplePlayerGUI: "+s);    }    // /////////////////////////// INITIALIZATION /////////////////////////////    /** Note: parent MUST be Displayable */    public SimplePlayerGUI() {	// initialize(String title, Parent parent) must be called after this    }    public void initialize(String title, Parent parent) {	this.title = title;	setParent(parent);	initialize();    }    public void setParent(Parent parent) {	this.parent = parent;	if (!(parent instanceof Displayable)) {	    throw new RuntimeException("parent must be instanceof Displayable!");	}	display = (Displayable) parent;	display.addCommand(backCommand);	display.setCommandListener(this);	updateTime();	updateRate(null);	updateTempo(null);	durationUpdated();	parent.updateKaraoke();	parent.updateDisplay();    }    private void initialize() {	karaokeMode = false;	karaokeLyrics = "";	currKaraokeLine = 0;	currKaraokeLinePos = 0;	currKaraokeLength = 0;	karaokeLines = null;	karaokeLinePos = null;	karaokeLineCount = 0;	redisplayKaraokeCounter = 0;	removeCommands();	setStatus("");	durationStr = "";	setSong("No song loaded", "");	guiTimer=new Timer();    }    public void setSong(String name, String locator) {	songLocator = locator;	songInputStream = null;	songContentType = "";	doSetSong(name);    }    public void setSong(String name, InputStream is, String contentType) {	songLocator = "";	songInputStream = is;	songContentType = contentType;	doSetSong(name);    }    public void setSong(String name, Player player) {	songLocator = "";	songInputStream = null;	songContentType = "";	doSetSong(name);	this.player = player;    }    private void doSetSong(String name) {	songName = name;	songDisplayNames = new String[1];	songDisplayNames[0] = name;	currSongDisplay = 0;	closePlayer();	setStatus("");	setFeedback("");	clearKaraoke();	updateTempo(null);	updateTime();	updateSongDisplay();    }    public void setTimerInterval(int millis) {	timerInterval = millis;    }    // /////////////////////////// VISUAL FEEDBACK ////////////////////////////    private void error(Throwable e) {	error(Utils.friendlyException(e));	if (Utils.DEBUG) e.printStackTrace();    }    private void error(String s) {	setFeedback(s);    }    private void setStatus(String s) {	parent.setStatus(s);	if (Utils.DEBUG) System.out.println("Status: "+s);    }    private void setFeedback(String s) {	parent.setFeedback(s);	if (Utils.DEBUG) System.out.println("Feedback: "+s);    }    private void updateKaraoke() {	parent.updateKaraoke();    }    private void updateTime() {	parent.updateTime();    }    private void updateRate(RateControl c) {	parent.updateRate();	// if the rate gauge is activated, update it	if (gauge != null && gaugeMode == GAUGE_RATE) {	    if (c==null) {		c = getRateControl();	    }	    if (c != null) {		String disp = getRateStr();		// or		// gauge.setValue((c.getRate() - c.getMinRate() + 1)/1000);		gauge.setValue((c.getRate() - c.getMinRate())/1000);		gaugeLabel.setLabel(disp);	    }	}    }    private void updateTempo(TempoControl c) {	parent.updateRate();	// if the tempo gauge is activated, update it	if (gauge != null && gaugeMode == GAUGE_TEMPO) {	    if (c==null) {		c = getTempoControl();	    }	    if (c != null) {		String disp = getTempoStr();		gauge.setValue((c.getTempo()/1000)-1);		gaugeLabel.setLabel(disp);	    }	}    }    private void updateVolume(VolumeControl c) {	if (c==null) {	    c = getVolumeControl();	}	if (c != null) {	    int l = c.getLevel();	    String disp = "Volume: "+String.valueOf(l);	    if (c.isMuted()) {		disp += " (muted)";	    }	    if (gauge != null && gaugeMode == GAUGE_VOLUME) {		gauge.setValue(l);		gaugeLabel.setLabel(disp);	    }	    setFeedback(disp);	}    }    private void updatePitch(PitchControl c) {	if (c==null) {	    c = getPitchControl();	}	if (c != null ) {	    String disp = "Transpose: "+toFloatString(c.getPitch(), 3)+" semi tones";	    // if the pitch gauge is activated, update it	    if (gauge != null && gaugeMode == GAUGE_PITCH) {		// WTK removed the next line		gauge.setValue((c.getPitch() - c.getMinPitch())/1000);		gaugeLabel.setLabel(disp);	    }	    setFeedback(disp);	}    }    private void updateLoop() {	if (player != null) {	    try {		int loop=1;		switch (loopmode) {		case LOOP_ONCE:		    loop=1;		    break;		case LOOP_3:		    loop=3;		    break;		case LOOP_INF:		    loop=-1;		    break;		}		boolean restart = false;		if (player.getState() == player.STARTED) {		    player.stop();		    restart = true;		}		player.setLoopCount(loop);		if (restart) {		    player.start();		}	    } catch (Exception e) {		error(e);	    }	}    }    private void updateSongDisplay() {	String add = "";	if (currSongDisplay == 0) {	    add = durationStr;	}	parent.setFileTitle(songDisplayNames[currSongDisplay] + add);    }    private void durationUpdated() {	if (player != null) {	    // include duration in song name	    try {	    	long duration = player.getDuration();		if (duration >= 0) {		    durationStr = " ("+timeDisplay(duration)+")";		} else {		    durationStr = "";		}	    } catch (IllegalStateException ise) {	    	// thrown in CLOSED state		durationStr = "";	    }	    updateSongDisplay();	}    }    private void clearKaraoke() {	karaokeLineCount = 0;	nextKaraokePos = -1;	redisplayKaraokeCounter = 0;	updateKaraoke();    }    // /////////////////////////// DISPLAY UTILITIES //////////////////////////    public Image getLogo() {	if (logo==null) {	    try {		logo = Image.createImage("/icons/logo.png");	    } catch (Exception ex) {		logo = null;	    }	    if (logo == null) {		debugOut("can not load logo.png");	    }	}	return logo;    }    private String toFloatString(int number, int digitsAfterDot) {	StringBuffer ret=new StringBuffer(String.valueOf(number));	while (ret.length()<(digitsAfterDot+1)) {	    ret.insert(0, '0');	}	ret.insert(ret.length()-digitsAfterDot, '.');	return ret.toString();    }    private String formatNumber(long num, int len, boolean leadingZeros) {	StringBuffer ret=new StringBuffer(String.valueOf(num));	if (leadingZeros) {	    while (ret.length()<len) {		ret.insert(0, '0');	    }	} else {	    while (ret.length()<len) {		ret.append('0');	    }	}	return ret.toString();    }    private String timeDisplay(long us) {	long ts = us/100000;	return formatNumber(ts/600l, 2, true)+":"+formatNumber(((ts % 600) / 10), 2, true)+"."+String.valueOf(ts % 10);    }    public String getMediaTimeStr() {	try {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -