Programming Java 2 Micro Edition on Symbian OS A developer’s guide to MIDP 2.0 phần 5 ppsx

50 374 0
Programming Java 2 Micro Edition on Symbian OS A developer’s guide to MIDP 2.0 phần 5 ppsx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

172 MIDP 2.0 AND THE JTWI public void itemStateChanged(Item item){ if (item instanceof Gauge){ Gauge volumeIndicator = (Gauge)item; int level = volumeIndicator.getValue(); audioPlayer.setVolume(level); } } The itemStateChanged() method obtains the value requested by the user and invokes the setVolume() method to adjust the audio playback volume via the Player’s VolumeControl The showAlert() method (see below) is called by the AudioPlayer instance in the event of an Exception being thrown at any stage of the Player lifecycle public void showAlert(String title, String message){ Alert alert = new Alert(title, message, null, AlertType.ERROR); display.setCurrent(alert, initialView); initialView.removeProgressGauge(); initialView.addCommand(playCommand); } After displaying an error Alert, the current Displayable is set to the InitialView allowing the user to try either the same URL again or a different URL The MIDletController class also provides a couple of callback methods: updateProgressGauge and removeVolumeControl The first updates the InitialView progress gauge as the Player progresses through its lifecycle The second removes the interactive volume indicator Gauge from the PlayerView in the event that the implementation of Player does not support a VolumeControl The full source code and JAR and JAD files for the Audio Player MIDlet can be downloaded from the Symbian website at www.symbian com/books 3.4.1.5 Working with Video We shall now illustrate how to play a video with code highlights taken from a simple Video Player MIDlet (see Figure 3.26) The architecture of the Video Player MIDlet (see Figure 3.27) is very similar to that of the Audio Player The MIDlet contains four classes: MIDletController, InitialView, VideoPlayer and VideoCanvas The VideoCanvas is used for rendering the video playback as well as providing controls similar to those found in the PlayerView of the Audio Player MIDlet The other classes fulfill very similar roles to their equivalents in the Audio Player MIDlet OPTIONAL J2ME APIS IN THE JTWI Figure 3.26 The Video Player MIDlet running on a Nokia Series 60 phone javax.microedition.lcdui.Form javax.microedition.lcdui.Canvas InitialView VideoCanvas MIDletController VideoPlayer javax.microedition.media.control.VideoControl javax.microedition.media.Player Figure 3.27 UML class diagram of the Video Player MIDlet The VideoPlayer Class Let’s first take a look at the key methods of the VideoPlayer class: import javax.microedition.media.*; import javax.microedition.media.control.*; import java.io.*; // Acquires the video content and renders it public class VideoPlayer implements Runnable { private final static String THREE_GPP = "3gp"; private final static String MPEG = "mpg"; 173 174 MIDP 2.0 AND THE JTWI private final static String MIME_3GPP = "video/3gpp"; private final static String MIME_MPEG = "video/mpeg"; private private private private private private private MIDletController controller; VideoCanvas canvas; Player player; VideoControl videoControl; String resource; String mimeType = THREE_GPP; Thread initializer; public VideoPlayer(MIDletController controller, VideoCanvas canvas){ } public void initializeVideo(String resource){ } public void run(){ } public void startPlayer(){ } public void stopPlayer(){ } public void closePlayer(){ } } The constructor is shown below public VideoPlayer(MIDletController controller, VideoCanvas canvas){ this.controller = controller; this.canvas = canvas; } It simply initializes the controller and canvas attributes with references to the MIDletController and the VideoCanvas respectively One difference between the Video Player and Audio Player MIDlets is that the Video Player obtains its content from resource files packaged in the MIDlet suite JAR file, rather than from a remote resource The initializeVideo() method takes the name of the video file as a parameter public void initializeVideo(String resource){ this.resource = resource; String fileExt = resource.substring(resource.lastIndexOf('.') + 1); if(fileExt.equals(THREE_GPP)) { mimeType = MIME_3GPP; } else if(fileExt.equals(MPEG)) { mimeType = MIME_MPEG; } initializer = new Thread(this); initializer.start(); } OPTIONAL J2ME APIS IN THE JTWI 175 The resource file name is tested to ascertain its format (MPEG for the Sun’s J2ME Wireless Toolkit 2.0 emulator and 3GPP for real phones) and the appropriate MIME type set A new thread is then launched to perform the essential initialization required to play the video content The run() method, mandated by the Runnable interface, contains the initialization of the Player public void run(){ try { InputStream in = getClass().getResourceAsStream("/" + resource); player = Manager.createPlayer(in, mimeType); player.addPlayerListener(controller); player.realize(); player.prefetch(); videoControl = (VideoControl)player.getControl("VideoControl"); if (videoControl != null) { videoControl.initDisplayMode( videoControl.USE_DIRECT_VIDEO, canvas); int cHeight = canvas.getHeight(); int cWidth = canvas.getWidth(); videoControl.setDisplaySize(cWidth, cHeight); videoControl.setVisible(true); startPlayer(); } else{ controller.showAlert("Error!", "Unable to get Video Control"); closePlayer(); } } catch (IOException ioe) { controller.showAlert("Unable to access resource", ioe.getMessage()); closePlayer(); } catch (MediaException me) { controller.showAlert("Unable to create player", me.getMessage()); closePlayer(); } } An InputStream is obtained from the resource file and used to create the Player instance A PlayerListener (the controller) is registered with the Player in order to receive callbacks The prefetch and realize() methods are then called on the Player instance Once the player is in the PREFETCHED state we are ready to render the video content First we must obtain a VideoControl by calling getControl on the Player, and casting it down appropriately Note that the MMAPI specification requires that a player for video media must support a VideoControl, unlike the case of a player for audio content, where support for VolumeControl is only a recommended practice 176 MIDP 2.0 AND THE JTWI The initDisplayMode() method is used to initialize the video mode that determines how the video is displayed This method takes an integer mode value as its first argument with one of two predefined values: USE_GUI_PRIMITIVE or USE_DIRECT_VIDEO In the case of MIDP implementations (supporting the LCDUI), USE_GUI_PRIMITIVE will result in an instance of a javax.microedition.lcdui.Item being returned: Item display = control.initDisplayMode(control.USE_GUI_PRIMITIVE, null); For CDC implementations supporting AWT, USE_GUI_PRIMITIVE will return an instance of java.awt.Component For implementations that support both LCDUI and AWT, the required type must be specified by a String as the second argument: Item display = control.initDisplayMode(control.USE_GUI_PRIMITIVE, "javax.microedition.lcdui.Item"); The USE_DIRECT_VIDEO mode can only be used with implementations that support the LCDUI (such as Symbian OS) and a second argument of type javax.microedition.lcdui.Canvas (or a subclass) must be supplied This is the approach adopted in the example code above Methods of VideoControl can be used to manipulate the size and the location of the video with respect to the Canvas where it will be displayed Since we are using direct video as the display mode it is necessary to call setVisible(true) in order for the video to be displayed (In the case of USE_GUI_PRIMITIVE the video is shown by default when the GUI primitive is displayed.) Finally, we start the rendering of the video with the startPlayer() method If at any stage an Exception is thrown the MIDletController.showAlert() method is called and the resources acquired by the Player are released by calling the closePlayer() method The other methods of the VideoPlayer class are the same as their namesakes in the AudioPlayer class of the Audio Player MIDlet The MIDletController Class The MIDletController class for the Video Player MIDlet is very similar to that of the Audio Player The method signatures of the class are shown below import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.media.*; // A simple video player MIDlet using JSR 135 - Mobile Media API public class MIDletController extends MIDlet implements OPTIONAL J2ME APIS IN THE JTWI 177 CommandListener, PlayerListener { private Command exitCommand, playCommand, backCommand, replayCommand; private Display display; private InitialView initialView; private VideoCanvas videoCanvas; private VideoPlayer videoPlayer; public MIDletController() { } public void startApp(){ } public void pauseApp(){ } public void destroyApp(boolean unconditional){ } public void commandAction(Command c, Displayable s){ } public void playerUpdate(Player p, String event, Object eventData) { } public void showAlert(String title, String message){ } public void releaseResources(){ } } The constructor is listed below: public MIDletController() { int noOfVideos = Integer.parseInt(getAppProperty( "number-of-videos")); String[] videoNames = new String[noOfVideos]; for (int i = 1; i

Ngày đăng: 12/08/2014, 23:22

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan