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.util.Vector;
037:
038: /**
039: * An example MIDlet to demo MMAPI video features
040: *
041: */
042: public class VideoTest extends MIDlet implements CommandListener,
043: Runnable {
044:
045: private static VideoCanvas videoCanvas = null;
046: private static VideoPlayer videoPlayer = null;
047: private static Vector videoClips;
048:
049: private Command exitCommand = new Command("Exit", Command.EXIT, 1);
050: private Command playCommand = new Command("Play", Command.ITEM, 1);
051: //private Command helpCommand = new Command("Help", Command.HELP, 1);
052:
053: //private Alert helpScreen = null;
054: private Display display;
055: private static List theList;
056: private static VideoTest instance = null;
057:
058: private static Vector urls;
059:
060: static public VideoTest getInstance() {
061: return instance;
062: }
063:
064: static public List getList() {
065: return theList;
066: }
067:
068: public VideoTest() {
069:
070: instance = this ;
071: display = Display.getDisplay(this );
072: theList = new List("MMAPI Video Player", Choice.IMPLICIT);
073: fillList();
074:
075: theList.addCommand(playCommand);
076: theList.addCommand(exitCommand);
077: theList.setCommandListener(this );
078: display.setCurrent(theList);
079: }
080:
081: private void fillList() {
082: videoClips = new Vector();
083: urls = new Vector();
084: for (int n = 1; n < 100; n++) {
085: String nthURL = "VideoTest-URL" + n;
086: String url = getAppProperty(nthURL);
087: if (url == null || url.length() == 0) {
088: break;
089: }
090: if (!SimplePlayer.isSupported(url))
091: continue;
092: String nthTitle = "VideoTest-" + n;
093: String title = getAppProperty(nthTitle);
094: if (title == null || title.length() == 0) {
095: title = url;
096: }
097: videoClips.addElement(title);
098: urls.addElement(url);
099: theList.append(title, null);
100: }
101: }
102:
103: /**
104: * Called when this MIDlet is started for the first time,
105: * or when it returns from paused mode.
106: * If there is currently a Form or Canvas displaying
107: * video, call its startApp() method.
108: */
109: public void startApp() {
110: //try {
111: if (videoPlayer != null)
112: videoPlayer.startApp();
113: if (videoCanvas != null)
114: videoCanvas.startApp();
115: //} catch (Exception e) {
116: // System.out.println("DEBUG: GOT EXCEPTION in VideoTest.startApp()!");
117: // e.printStackTrace();
118: //}
119: }
120:
121: /**
122: * Called when this MIDlet is paused.
123: * If there is currently a Form or Canvas displaying
124: * video, call its startApp() method.
125: */
126: public void pauseApp() {
127: //try {
128: if (videoPlayer != null)
129: videoPlayer.pauseApp();
130: if (videoCanvas != null)
131: videoCanvas.pauseApp();
132: //} catch (Exception e) {
133: // System.out.println("DEBUG: GOT EXCEPTION in VideoTest.pauseApp()!");
134: // e.printStackTrace();
135: //}
136: }
137:
138: /**
139: * Destroy must cleanup everything not handled
140: * by the garbage collector.
141: */
142: public synchronized void destroyApp(boolean unconditional) {
143: //try {
144: if (videoPlayer != null)
145: videoPlayer.close();
146: if (videoCanvas != null)
147: videoCanvas.close();
148: nullPlayer();
149: //} catch (Exception e) {
150: // System.out.println("DEBUG: GOT EXCEPTION in VideoTest.destroyApp("+unconditional+")!");
151: // e.printStackTrace();
152: //}
153: }
154:
155: public synchronized void nullPlayer() {
156: videoPlayer = null;
157: videoCanvas = null;
158: }
159:
160: int index = 0;
161:
162: public void run() {
163: //try {
164: if (index % 2 == 0) {
165: videoPlayer = new VideoPlayer(display);
166: videoPlayer.open((String) urls.elementAt(index));
167: if (videoPlayer != null) {
168: display.setCurrent(videoPlayer);
169: videoPlayer.start();
170: }
171: } else {
172: videoCanvas = new VideoCanvas(display);
173: videoCanvas.open((String) urls.elementAt(index));
174: if (videoCanvas != null) {
175: display.setCurrent(videoCanvas);
176: videoCanvas.start();
177: }
178: }
179: //} catch (Exception e) {
180: // System.out.println("DEBUG: GOT EXCEPTION in VideoTest.run()!");
181: // e.printStackTrace();
182: //}
183: }
184:
185: /*
186: * Respond to commands, including exit
187: * On the exit command, cleanup and notify that the MIDlet has
188: * been destroyed.
189: */
190: public void commandAction(Command c, Displayable s) {
191: //try {
192: if (c == exitCommand) {
193: synchronized (this ) {
194: if (videoPlayer != null || videoCanvas != null) {
195: new ExitThread().start();
196: } else {
197: destroyApp(false);
198: notifyDestroyed();
199: }
200: }
201: } else if ((s == theList && c == List.SELECT_COMMAND)
202: || c == playCommand) {
203: synchronized (this ) {
204: if (videoPlayer != null || videoCanvas != null) {
205: return;
206: }
207: int i = theList.getSelectedIndex();
208: index = i;
209: // need to start the players in a separate thread to
210: // not block the command listener thread during
211: // Player.realize: if it requires a security
212: // dialog (like "is it OK to use airtime?"),
213: // it would block the VM
214: (new Thread(this )).start();
215: }
216: }
217: //} catch (Exception e) {
218: // System.out.println("DEBUG: GOT EXCEPTION in VideoTest.commandAction("+c.toString()+","+s.toString()+")!");
219: // e.printStackTrace();
220: //}
221: }
222:
223: class ExitThread extends Thread {
224: public void run() {
225: //try {
226: // this is stop()+deallocate(), but not close(),
227: //which is done in destroyApp() ...
228: if (videoPlayer != null) {
229: videoPlayer.stopVideoPlayer();
230: //videoPlayer = null;
231: } else { //videoCanvas != null
232: videoCanvas.stopVideoCanvas();
233: //videoCanvas = null;
234: }
235: destroyApp(false);
236: notifyDestroyed();
237: //} catch (Exception e) {
238: // System.out.println("DEBUG: GOT EXCEPTION in VideoTest.ExitThread.run()!");
239: // e.printStackTrace();
240: //}
241: }
242: }
243: }
|