001: /*
002: *
003: * Copyright (c) 2007, Sun Microsystems, Inc.
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * * Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * * Neither the name of Sun Microsystems nor the names of its contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032: package example.audiodemo;
033:
034: import javax.microedition.lcdui.*;
035: import javax.microedition.media.*;
036: import javax.microedition.midlet.*;
037:
038: public class MixTest extends MIDlet implements CommandListener,
039: Runnable {
040: private static final String[] mcases = { "Tone+Wav",
041: "Tone+ToneSeq", "ToneSeq+Wav" };
042: static String wavUrl;
043: private static MixCanvas soundObj = null;
044: private static List theList;
045: private static MixTest instance = null;
046: private Command exitCommand = new Command("Exit", Command.EXIT, 1);
047: private Command playCommand = new Command("Play", Command.ITEM, 1);
048: private Display display;
049:
050: // if this MIDlet's startApp method is started
051: // for the first time
052: private boolean firstTime = true;
053:
054: // pause/resume support
055: private boolean restartOnResume = false;
056:
057: public MixTest() {
058: instance = this ;
059: display = Display.getDisplay(this );
060: theList = new List("Lists", Choice.IMPLICIT);
061:
062: for (int i = 0; i < mcases.length; i++) {
063: theList.append(mcases[i], null);
064: }
065:
066: wavUrl = getAppProperty("MixTestURL");
067: theList.addCommand(playCommand);
068: theList.addCommand(exitCommand);
069: theList.setCommandListener(this );
070: soundObj = new MixCanvas(display);
071: }
072:
073: public static MixTest getInstance() {
074: return instance;
075: }
076:
077: public static List getList() {
078: return theList;
079: }
080:
081: /**
082: * Called when this MIDlet is started for the first time,
083: * or when the MIDlet returns from paused mode.
084: *
085: * If it is the first time, display the menu list.
086: *
087: * Otherwise, if music was playing when the MIDlet
088: * was paused, call the demo's playSound method.
089: */
090: public void startApp() {
091: if (firstTime) {
092: display.setCurrent(theList);
093: firstTime = false;
094: } else {
095: if ((soundObj != null) && restartOnResume) {
096: soundObj.playSound();
097: }
098:
099: restartOnResume = false;
100: }
101: }
102:
103: /**
104: * Called when this MIDlet is paused.
105: * Pause the thread.
106: * If the demo is playing, call its pause method.
107: * For consistency across different VM's,
108: * it's a good idea to stop the player if
109: * it's currently playing.
110: */
111: public void pauseApp() {
112: restartOnResume = ((soundObj != null) && soundObj.isPlaying());
113:
114: if (restartOnResume) {
115: soundObj.pauseSound();
116: }
117: }
118:
119: /**
120: * Destroy must cleanup everything not handled
121: * by the garbage collector.
122: */
123: public void destroyApp(boolean unconditional) {
124: if (soundObj != null) {
125: soundObj.stopSound();
126: }
127:
128: soundObj = null;
129: }
130:
131: public void commandAction(Command c, Displayable s) {
132: if (c == exitCommand) {
133: destroyApp(false);
134: notifyDestroyed();
135: } else if (((s == theList) && (c == List.SELECT_COMMAND))
136: || (c == playCommand)) {
137: int i = theList.getSelectedIndex();
138: soundObj.setIndex(i);
139: display.setCurrent(soundObj);
140: soundObj.serviceRepaints();
141:
142: // method playSound() should not be invoked on GUI thread. Manager.createPlayer()
143: // will potentially invoke a blocking I/O. This is not good
144: // practice recommended by MIDP programming style. So here we
145: // will create the Player in a separate thread.
146: new Thread(this ).start();
147: }
148: }
149:
150: public void run() {
151: if (soundObj != null) {
152: soundObj.playSound();
153: }
154: }
155: }
|