001: /*
002: * Copyright (c) 2007, Sun Microsystems, Inc.
003: *
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * * Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: * * Neither the name of Sun Microsystems, Inc. nor the names of its
017: * contributors may be used to endorse or promote products derived
018: * from this software 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 A
023: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
024: * OR 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.mmademo;
033:
034: import javax.microedition.midlet.*;
035: import javax.microedition.lcdui.*;
036: import java.io.IOException;
037: import javax.microedition.media.*;
038:
039: /**
040: * Demonstrates how to properly respond to pauseApp()
041: */
042: public class PausingAudioTest extends MIDlet implements CommandListener {
043:
044: private Command behaveCommand = new Command("Behave",
045: Command.SCREEN, 2);
046: private Command misbehaveCommand = new Command("Misbehave",
047: Command.SCREEN, 2);
048: private Command exitCommand = new Command("Exit", Command.BACK, 1);
049:
050: private Form screen = null;
051:
052: private Player p = null;
053:
054: private boolean isWellBehaved = true;
055:
056: public PausingAudioTest() {
057: }
058:
059: public void startApp() {
060: if (screen == null) {
061: screen = new Form("Pausing Audio Test");
062: screen.addCommand(exitCommand);
063: screen.setCommandListener(this );
064: Display.getDisplay(this ).setCurrent(screen);
065: }
066: setupScreen();
067: if (p == null) {
068: String url = getAppProperty("PauseAudioURL");
069: try {
070: p = Manager.createPlayer(url);
071: } catch (IOException ioe) {
072: screen
073: .append(new StringItem("Could not open URL:",
074: url));
075: screen.append(new StringItem("Exception:", ioe
076: .toString()));
077: return;
078: } catch (MediaException me) {
079: screen.append(new StringItem("Manager.createPlayer("
080: + url + " threw:", me.toString()));
081: return;
082: }
083: p.setLoopCount(-1);
084: }
085: try {
086: p.start();
087: } catch (MediaException me) {
088: screen.append(new StringItem("Player.start() threw:", me
089: .toString()));
090: }
091: }
092:
093: private void setupScreen() {
094: screen.deleteAll();
095: screen.removeCommand(behaveCommand);
096: screen.removeCommand(misbehaveCommand);
097:
098: if (isWellBehaved) {
099: screen.addCommand(misbehaveCommand);
100: screen.append(new StringItem("Current State:",
101: "Well-Behaved"));
102: } else {
103: screen.addCommand(behaveCommand);
104: screen.append(new StringItem("Current State:",
105: "Not Well-Behaved"));
106: }
107: }
108:
109: public void pauseApp() {
110: if (isWellBehaved && p != null) {
111: try {
112: p.stop();
113: } catch (MediaException me) {
114: screen.append(new StringItem("Player.stop() threw:", me
115: .toString()));
116: }
117: }
118: }
119:
120: public void destroyApp(boolean unconditional) {
121: if (p != null) {
122: p.close();
123: }
124: }
125:
126: public void commandAction(Command c, Displayable s) {
127: if (c == behaveCommand || c == misbehaveCommand) {
128: isWellBehaved = !isWellBehaved;
129: setupScreen();
130: } else if (c == exitCommand) {
131: destroyApp(true);
132: notifyDestroyed();
133: }
134: }
135: }
|