Advertisment

Develop Rich Multimedia Apps with J2ME

author-image
PCQ Bureau
New Update

Sun Microsystems claims that Java ME is the most ubiquitous application

platform for mobile devices, deployed on more than a billion sets. Apart from

the great advantage of portability across devices along with robust security,

Java ME provides a rich set of APIs for today's multimedia and web enabled

mobile handsets. Some of the useful libraries/APIs provided by the Java ME

platform are:

Advertisment
  • Wireless Messaging API
  • Mobile Media API
  • JAXP XML Parser
  • Location API
  • Mobile 3D Graphics
  • Payment API

In the article “Creating Apps for Mobiles”, published in PCQ Jan'09, we

explored the J2ME development tools to create a simple application. We will

continue the exploration by creating a tiny video player using the Mobile Media

API.

Direct Hit!

Applies To: Adv Java developers



USP: Develop feature-rich multimedia apps
for 3G networks



Primary Link: java.sun.com


Keywords:
J2ME, 3G
Advertisment

Mobile Media API (JSR 135)



The Mobile Media API provides an interface to the multimedia capabilities of

the mobile device running J2ME. This includes the speaker (or headset),

microphone, LCD Screen (for video) and Camera (if available).

The package name for the Mobile Media API is:

“javax.microedition.media”.

Advertisment

The building blocks of the system consist of — Manager, Player and Control.

The Manager is the top level controller for the multimedia resources. J2ME

applications use the Manager to create and manage Players and query status and

properties of the Players. The Players actually play the multimedia content. The

Control interface is used to implement the different controls a Player might

have, such as VolumeControl to increase/decrease the volume.

Create a tiny video player application



In this article we will go through the steps of creating a project using the

Mobile Media API and then develop the code required to create the player. Please

refer to the PCQ Jan '09 issue (also available online at pcquest.com) for the

procedure to install the tools. You can test this application on the Emulator,

but to use it on a mobile device, you will need a data connection such as GPRS

or the blazing fast 3G.

We will start by creating a project with the following details:

Advertisment

The toolkit will create the project and show the paths where you have to

create the source files.

Creating the source file for PCQ_Player Application



We now write the code to provide the desired functionality to your application:

Advertisment

package com.j2me.part1;



import javax.microedition.lcdui.*;


import javax.microedition.midlet.MIDlet;


import javax.microedition.media.*;


import javax.microedition.media.control.*;


public class PCQ_Player extends MIDlet implements



CommandListener {

private Display display;



private Form form;


VideoPlayer vPlayer;


public PCQ_Player()


{


display = Display.getDisplay(this);


form = new Form("PCQ Player");


}


public void startApp() {


form.addCommand( new Command( "Exit", Command.EXIT, 1 ));


form.setCommandListener(this);


vPlayer = new VideoPlayer(display, form, "http://createapoll.net/guest/R1.mpg");


Thread runner = new Thread(vPlayer);


runner.start();


}












Advertisment

public void pauseApp() {



}


public void destroyApp(boolean unconditional) {


}

New Project

Dialog



Project Name:


PCQ_Player


MIDlet Class Name: com.j2me.part1


.PCQ_Player


API selection

dialog:



Target Platform: JTWI


Configurations: CLDC 1.1


Optional: select the “Mobile Media API”

public void commandAction(Command command, Displayable

disp)



{


vPlayer.playerClose();


destroyApp(true);


notifyDestroyed();


}


}




Advertisment

class VideoPlayer implements Runnable, PlayerListener



{


Form form;


Player player;


Display display;


String url;


public VideoPlayer(Display display, Form form, String url)


{


this.display = display;


this.form = form;


this.url = url;


}


public void run() {


try {


Alert alert = new Alert("Buffering ..."); //Download takes time


alert.setTimeout(Alert.FOREVER); //so display message


display.setCurrent(alert);


player = Manager.createPlayer(url);


player.addPlayerListener(this);


player.setLoopCount(1);


//Play only once


player.start(); // Start the playback


display.setCurrent(form);


}


catch(Exception e)


{


Alert alert = new Alert("Failed to play video"); //Display message in case


alert.setTimeout(Alert.FOREVER); //there is an error


display.setCurrent(alert);


}


}


public void playerUpdate(Player player, String event, Object eventData)


{


if(event.equals(PlayerListener.STARTED)


)


{


videoControl vControl = (VideoControl) player.getControl("VideoControl");


form.append((Item)v Control.initDisplayMode(vControl.USE_GUI_PRIMITIVE, null));


} else if(event.equals(PlayerListener.CLOSED))


{


form.deleteAll();


}


}








































public void playerClose() {



player.close(); // close playback


}


}

Copy the above source code in Notepad and save this as PCQ_Player.java in the

fully qualified source path, which is the path you copied in the previous step

plus 'com\j2me\part1'. In my case the path is

D:\Users\Sudipto Chanda\ j2mewtk\ 2.5.2\ apps\PCQ_Player\src\

com\j2me\ part1.

Important: Make sure that you select 'All Files', while saving from

Notepad, otherwise, Notepad may append a '.txt' extension. It is a good idea to

verify the file name to be PCQ_Player.java.

The source given in this article is very minimal without much error handling.

The documentation provided with WTK (usually located in C:\WTK2.5.2\docs\ api\midp)

contains more detailed guidelines of using the Mobile Media API and other

packages of J2ME.

Executing the PCQ_Player



Use the Build option to compile and build the player source. Then you need

to click on Run to execute the code in the Emulator. Next click on the button

that is located below 'Launch' to start the PCQ_Player application.

The Emulator will present you with the question “Is it OK to Use Airtime?”

You need to click on the button below 'Yes' to continue. This option is valuable

in mobile phones as service providers will charge you for data downloads. You

might need to be patient though, as your video will take a while to come up,

depending on your computer and Internet speed.



The PCQ_Player application plays the content from the URL:

http://createapoll. net/guest/R1.mpg”.

Now, you can upload content to any website where you have access. However,

the content should be converted for the size and capability of the device. Files

with MPEG1/MPEG2 video



with MP3 audio in QVGA or QCIF sizes usually work fine with this application.

Conclusion



As you have seen, J2ME can be used to create professional quality applications
for rich multimedia and graphics experience. You can also extend this

functionality to create robust enterprise class applications for banking and

finance.

Read more at...
  • Mobile Media API (JSR 135)

    Specifications: http://tinyurl.com/dddkur.
  • There are many demo

    applications provided with the toolkit (look at C:\WTK2.5.2\ apps).

These days you see a lot of banks and financial institutions giving

importance to the mobile banking platform. With millions of mobile devices being

sold around the world that support the Java ME platform, along with the spread

of Internet connection for mobile devices such as GPRS, Edge and the blazingly

fast 3G and WiMAX just round the corner, the possibilities for developers

providing apps for these platforms are immense.

The core advantage of Java ME — which is portability across platforms (such

as Windows Mobile, Symbian, Linux etc.) makes it a winner in the diverse jungle

of embedded systems.

Sudipto Chanda

Advertisment