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 java.io.*;
035:
036: import java.util.*;
037:
038: import javax.microedition.lcdui.*;
039: import javax.microedition.media.*;
040: import javax.microedition.media.control.*;
041: import javax.microedition.midlet.*;
042:
043: /**
044: * This is a demo midlet to show the basic audio functionalities, to
045: * play wave file, tone, tone sequence from http, resource jar file
046: * and record store.
047: *
048: * @version
049: *
050: */
051: public class AudioPlayer extends MIDlet implements CommandListener {
052: private static PlayerCanvas playerGUI = null;
053: private static List theList;
054: private static Vector urls;
055: private Command exitCommand = new Command("Exit", Command.EXIT, 1);
056: private Command playCommand = new Command("Play", Command.ITEM, 1);
057: private Display display;
058:
059: // pause/resume support
060: private boolean restartOnResume = false;
061:
062: public AudioPlayer() {
063: super ();
064: display = Display.getDisplay(this );
065: initPlayList();
066: display.setCurrent(theList);
067: }
068:
069: public static List getList() {
070: return theList;
071: }
072:
073: /**
074: * Called when this MIDlet is started for the first time,
075: * or when it returns from paused mode.
076: * If a player is visible, and it was playing
077: * when the MIDlet was paused, call its playSound method.
078: */
079: public void startApp() {
080: if ((playerGUI != null) && restartOnResume) {
081: playerGUI.playSound();
082: }
083:
084: restartOnResume = false;
085: }
086:
087: /**
088: * Called when this MIDlet is paused.
089: * If the player GUI is visible, call its pauseSound method.
090: * For consistency across different VM's
091: * it's a good idea to stop the player if it's currently
092: * playing.
093: */
094: public void pauseApp() {
095: restartOnResume = ((playerGUI != null) && playerGUI.isPlaying());
096:
097: if (restartOnResume) {
098: playerGUI.pauseSound();
099: }
100: }
101:
102: /**
103: * Destroy must cleanup everything not handled
104: * by the garbage collector.
105: */
106: public void destroyApp(boolean unconditional) {
107: if (playerGUI != null) {
108: playerGUI.stopSound();
109: playerGUI = null;
110: }
111:
112: display.setCurrent(null);
113: }
114:
115: public void commandAction(Command c, Displayable s) {
116: if (c == exitCommand) {
117: destroyApp(true);
118: notifyDestroyed();
119: } else if (((s == theList) && (c == List.SELECT_COMMAND))
120: || (c == playCommand)) {
121: int i = theList.getSelectedIndex();
122:
123: if (i == 0) { // Simple tone
124:
125: try {
126: Manager.playTone(60, 200, 90);
127: } catch (MediaException ex) {
128: System.out.println("can't play tone");
129: }
130: } else if (i > 0) {
131: if (playerGUI == null) {
132: playerGUI = new PlayerCanvas(display);
133: } else {
134: playerGUI.stopSound();
135: }
136:
137: playerGUI.setParam((String) urls.elementAt(i));
138: playerGUI.playSound();
139: display.setCurrent(playerGUI);
140: }
141: }
142: }
143:
144: /**
145: * load the play list from thd jad file
146: *
147: **/
148: private void initPlayList() {
149: urls = new Vector();
150:
151: theList = new List("MIDP Audio Player", Choice.IMPLICIT);
152:
153: for (int n = 1; n < 32; n++) {
154: String nthURL = "PlayerURL-" + n;
155: String url = getAppProperty(nthURL);
156:
157: if ((url == null) || (url.length() == 0)) {
158: break;
159: }
160:
161: String nthTitle = "PlayerTitle-" + n;
162: String title = getAppProperty(nthTitle);
163:
164: if ((title == null) || (title.length() == 0)) {
165: title = url;
166: }
167:
168: urls.addElement(url);
169: theList.append(title, null);
170: }
171:
172: theList.addCommand(exitCommand);
173: theList.addCommand(playCommand);
174: theList.setCommandListener(this);
175: }
176: }
|