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.midlet.*;
036:
037: /*
038: * this is a sample program to show ring tones synchronized with
039: * selected background music.
040: * It is based on the manyballs midlet. When a ball hits a wall,
041: * a ring tone is heard.
042: *
043: * Usage: Use right arrow key to add one more ball (5 is the maximum).
044: * Use left arrow key to delete one ball
045: * Use up arrow key to increase the speed of ball
046: * Use down arrow key to reduce the speed of ball
047: *
048: * @version
049: */
050: public class BBall extends MIDlet implements CommandListener, Runnable {
051: private static final String[] bgs = { "no background",
052: "wave background", "tone seq background", "MIDI background" };
053: static String wavbgUrl;
054: static String midbgUrl;
055: private static final Object gameLock = new Object();
056: private Command exitCommand = new Command("Exit", Command.EXIT, 1);
057: private Command playCommand = new Command("Play", Command.ITEM, 1);
058: private Display display;
059: private int idx = 0;
060: private List theList;
061: private BallCanvas game;
062:
063: // if this MIDlet's startApp method is started
064: // for the first time
065: private boolean firstTime = true;
066:
067: // pause/resume support
068: private boolean restartOnResume = false;
069:
070: public BBall() {
071: display = Display.getDisplay(this );
072: theList = new List("Bouncing Ball", Choice.IMPLICIT);
073:
074: for (int i = 0; i < bgs.length; i++) {
075: theList.append(bgs[i], null);
076: }
077:
078: wavbgUrl = getAppProperty("BBall-wav-URL");
079: midbgUrl = getAppProperty("BBall-MIDI-URL");
080: theList.addCommand(playCommand);
081: theList.addCommand(exitCommand);
082: theList.setCommandListener(this );
083: }
084:
085: // also called from BallCanvas
086: public void displayList() {
087: display.setCurrent(theList);
088: }
089:
090: /**
091: * Called when this MIDlet is started for the first time,
092: * or when the MIDlet returns from paused mode.
093: *
094: * If it is the first time, display the menu list.
095: *
096: * Otherwise, wake up the thread. If music was playing
097: * when the MIDlet was paused, call the game's
098: * start method.
099: */
100: public void startApp() {
101: if (firstTime) {
102: displayList();
103: firstTime = false;
104: } else {
105: SmallBall.paused = false;
106:
107: if ((game != null) && restartOnResume) {
108: game.start();
109: }
110:
111: restartOnResume = false;
112: }
113: }
114:
115: /**
116: * Called when this MIDlet is paused.
117: * Pause the thread.
118: * If the game is not already paused, call its pause method.
119: * For consistency across different VM's,
120: * it's a good idea to stop the player if
121: * it's currently playing.
122: */
123: public void pauseApp() {
124: SmallBall.paused = true;
125: restartOnResume = ((game != null) && !game.isPaused());
126:
127: if (restartOnResume) {
128: game.pause();
129: }
130: }
131:
132: /**
133: * Destroy must cleanup everything not handled
134: * by the garbage collector.
135: */
136: public void destroyApp(boolean unconditional) {
137: synchronized (gameLock) {
138: if (game != null) {
139: game.destroy();
140: }
141: }
142: }
143:
144: /*
145: * Respond to commands, including exit
146: * On the exit command, cleanup and notify that the MIDlet has
147: * been destroyed.
148: */
149: public void commandAction(Command c, Displayable s) {
150: if (c == exitCommand) {
151: synchronized (gameLock) {
152: if (game != null) {
153: game.destroy();
154: game = null;
155: }
156: }
157:
158: destroyApp(false);
159: notifyDestroyed();
160: } else if (((s == theList) && (c == List.SELECT_COMMAND))
161: || (c == playCommand)) {
162: idx = theList.getSelectedIndex();
163: new Thread(this ).start();
164: }
165: }
166:
167: public void run() {
168: synchronized (gameLock) {
169: if (game == null) {
170: game = new BallCanvas(this );
171: }
172:
173: display.setCurrent(game);
174: game.init(8, idx);
175:
176: if (game.needAlert()) {
177: Alert alert = new Alert("Warning",
178: "Cannot create player", null, null);
179: alert.setTimeout(1000);
180: display.setCurrent(alert, game);
181: }
182:
183: game.start();
184: }
185: }
186: }
|