Slater, S. (2007) JAVA based MIDP 2.0 tutorials: Tutorial 5

advertisement
Complementary Mobility
Building a multimedia application with MIDP
Table of Contents
Introduction .............................................................................................................................................. 2
Multimedia Application Considerations ................................................................................................... 2
Web Application Considerations .............................................................................................................. 3
Playing Sounds in a Multimedia Application............................................................................................. 4
Playing Video in a Multimedia Application ............................................................................................... 7
Using HTTP to retrieve a web page ........................................................................................................... 8
5: Building a multimedia application with MIDP
Page: 1
Complementary Mobility
Building a multimedia application with MIDP
Introduction
The purpose of this document is to explain how to create a basic multimedia application using Java and
the MIDP library, in particular the MIDP 2.0 library. This documents also aims to explain the principles of
building web applications using Java and the MIDP library. This introduction assumes you are using
Windows XP as your host PC operating system.
Articles in this series include:





1: Introduction to Mobile Application Development
2: Beginning application development with MIDP
3: Transferring an application Midlet to a mobile device
4: Building an interactive application Midlet with MIDP
5: Building a multimedia application with MIDP
Multimedia Application Considerations
Multimedia is a generic term that encompasses all aspects of media including text, images (moving/still,
mono/gray/colour, 2D/3D), audio and web services. Although mobile devices are becoming better at
handling these different aspects of multimedia there are still many devices in use, especially older ones,
that do not support many of these aspects and others can be severely limited. With this in mind MIDP
2.0 supports natively only certain aspects of audio however additional libraries exist such as the Mobile
Media API (MMAPI) that can handle many more – you should always check your intended target
platform.
The MMAPI has some advanced functionality and uses several components which can work together to
provide a feature filled multimedia experience.Using MMAPI to obtain information from the device you
can issue the following command:
// Query the device for support media types
String strTypes[] = Manager.getSupportedContentTypes(null);
// Loop through the supported types and output them to the debug window
for(int i=0; i<strTypes.length; i++) System.out.println(strTypes[i]);
This will return a collection of strings containing all the support media content types that the device can
handle, e.g. audio/midi, audio/x-wav, video/mpeg and video/mp4. The list of supported types using the
default colour emulator produced this list:
video/mpeg
image/gif
audio/x-wav
audio/amr
audio/x-tone-seq
video/vnd.sun.rgb565
audio/sp-midi
audio/midi
5: Building a multimedia application with MIDP
Page: 2
Complementary Mobility
Building a multimedia application with MIDP
This information can be used to assess which media formats a particular device can handle so you can
take the appropriate action within the application. A list of media types can be obtained from the
Internet Assigned Numbers Authority (IANA) website at http://www.iana.org/assignments/mediatypes/.
Web Application Considerations
There are two important aspects to consider when designing network applications, in particular Internet
based applications, and these are cost and security. Network access on many mobile tariffs can still incur
charges based on data throughput, and security concerns over any network based application are still an
important factor in developing web based applications and firewalls and other security tools can often
severely hinder the performance of an application which it deems inappropriate. In addition different
methods for developing mobile web based applications exist including WAP, HTTP, HTML and XML,
XHTML so choosing the right platform is essential.
5: Building a multimedia application with MIDP
Page: 3
Complementary Mobility
Building a multimedia application with MIDP
Playing Sounds in a Multimedia Application
In previous articles we already discussed how to place textual elements and simple images upon the
device display. These were using the Image Class and the append and textField methods of the Form
Class. To enhance or comlement the images displayed it may be desirable to add sound elements to an
application and there are a number of ways of achieving this in mobile applications.
In our myApp application, a simple way to add sound would be to use the AlertType Class and use
something similar to:
// play an alert
AlertType.INFO.playSound(myDisplay);
Instead of INFO we could use ALARM, CONFIRMATION, ERROR or WARNING. However, these sounds are
primitive and are designed to accompany a specific Alert message. There is also little control over the
sound itself and relies heavily on the underlying operating system interpretation of a particular sound. A
better alternative would be to use the media functions available within MIDP 2.0 especially the Mobile
Media API (MMAPI). In its simplest form we could play a sound by utilising the Manager Class of MMAPI
in the following code:
// play first tone
Manager.playTone(50,
100, 50);
// play second tone
Manager.playTone(100, 100, 50);
This will play two tones (tones are integer values between zero and 127), both for 100ms at half the
volume level (volume level is an integer from 1 to 100). However, it can be quite tricky (and time
consuming) coding all the relevant tones and other values into a program so you may want to play a preprepared sound or music file like a .wav or .mid file instead. To play a music file you first need to create
a player based on the Player Class in conjunction with the Manager Class.
public static Player createPlayer(java.io.InputStream stream,
java.lang.String type)
throws java.io.IOException,
MediaExceptionThrows:
You will also need to import the relevant libraries:
import
import
import
import
import
javax.microedition.media.Player;
javax.microedition.media.Manager;
javax.microedition.media.MediaException;
java.io.IOException;
java.io.InputStream;
If you want to repeat a sound multiple times, you can use the setLoopCount method of the Player Class,
passing this an integer with loop the sound any number of times and to loop the sound continuously
simply pass the value -1.
5: Building a multimedia application with MIDP
Page: 4
Complementary Mobility
Building a multimedia application with MIDP
myPlayer.setLoopCount(-1);
You should also use the realize method prior to starting playing in order that any suitable connection to
the source stream can be prepared. Performing a realize (and the optional prefetch) early on in an
application can ensure that media is ready to play when the start command is issued.
You should also encapsulate the createPlayer with a try and catch pair in case initialisation fails.
Player myPlayer;
InputStream myInputStream;
try {
myInputStream = getClass().getResourceAsStream("music.wav");
myPlayer = Manager.createPlayer(myInputStream,"audio/X-wav");
myPlayer.realize();
myPlayer.start();
}
catch(MediaException errME) { /* deal with exception here */}
catch(IOException errIOE) { /* deal with exception here */}
To pause the player you can issue the command:
myPlayer.stop();
and to stop the player completely issue the command:
myPlayer.close();
5: Building a multimedia application with MIDP
Page: 5
Complementary Mobility
Building a multimedia application with MIDP
You may also want to adjust the volume within the application, this can be achieved using the
VolumeControl Class.
import javax.microedition.media.control.VolumeControl;
// define the volume controller
VolumeControl myVolumeControl;
// create an instance of the volume control
myVolumeControl = (VolumeControl) myPlayer.getControl("VolumeControl");
// set the volume
if(myVolumeControl != null) myVolumeControl.setVolume(100);
5: Building a multimedia application with MIDP
Page: 6
Complementary Mobility
Building a multimedia application with MIDP
Playing Video in a Multimedia Application
Using a series of images that are quickly overlaid upon each other - a sense of animation can be
achieved. However, it may be desirable to play a video clip from within an application. Playing video
clips is very reliant on the underlying device and performance will vary greatly from device to device.
However, playing a video can be achieved using the Manager Class of MMAPI and it works in a similar
way to playing an audio file. You should also consider the size of the video file as it will most likely need
to be packaged with the .jar file (unless it is downloadable using a network connection) which can have
an impact on application performance and dramatically increase the final size of the .jar file. You should
run get getSupportedContentType method (described in the introduction) on any video file you run to
ensure that the device supports the encoding format of your source file.
The first step to play a video clip is to create a video controller object, this can be created by requesting
one from the previously defined media player.
import javax.microedition.media.control.VideoControl;
// Declare our video controller
VideoControl myVideoControl;
// Ask the player for a video controller
myVideoControl = (VideoControl) myPlayer.getControl("VideoControl");
The next step is to tell the application how to draw the video, use this command to enable the video clip
to play on a form:
// use the assisted library to play the video
myVideoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, this);
// add video controller to the form
myForm.append((Item)myVideoControl);
As an alternative you could also issue the following command to use the video control on a Canvas.
// use the devices accelerated video routine
myVideoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);
// add video controller to the canvas
myCanvas.append((Item)myVideoControl);
You can now load and play the video clip using the same routines as those in the audio section.
5: Building a multimedia application with MIDP
Page: 7
Complementary Mobility
Building a multimedia application with MIDP
Using HTTP to retrieve a web page
At its simplest level a web page can be retrieved by utilising the HyperText Transport Protocol (HTTP)
using the Connector Class, the following program demonstrates the Connector Class in action by
establishing a connector to a web page and retrieving up to the first 1024 characters and displays them
in the output window of the toolkit.
// A Simple Java Midlet to read a Web Page
// import necessary libraries
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
// Class for appNet
public class appNet extends Canvas implements Runnable
{
public Thread
appThread;
public HttpConnection myConnection;
public InputStream
myStream;
public byte[]
dataStream;
public appNet(myApp3 inParent)
{} // appNet constructor
// Update the screen from the buffer
public void paint(Graphics g)
{} // method paint()
// keep running the application until it is terminated
public void run()
{
try
{
// create a datastream to hold the received data
dataStream = new byte[1024];
// Attempt to open a connection to a given URL
myConnection = (HttpConnection)Connector.open("http://www.google.com");
// Attempt to open the input stream
myStream = myConnection.openInputStream();
// calculate the length of the datastream
int readLength = myStream.read(dataStream);
// convert the datastream to a string and display it
System.out.println(new String(dataStream, 0, readLength));
} // try
catch (Exception err)
{
// output any exceptions to the debug window
System.out.println("Exception: " + err.toString());
} // catch
finally
{
if (myStream != null)
{
try { myStream.close(); }
catch
(IOException
errIOE)
{
System.out.println("Exception:
errIOE.toString()); }
} // if
if (myConnection != null)
{
try { myConnection.close(); }
5: Building a multimedia application with MIDP
"
+
Page: 8
Complementary Mobility
Building a multimedia application with MIDP
catch
(IOException
errIOE.toString()); }
} // if
} // finally
} // method run()
errIOE)
{
System.out.println("Exception:
"
+
synchronized void start()
{
appThread = new Thread(this);
appThread.start();
} // method start
} // Class appNet
When the program is run you will need to select the Menu soft button, then Start. You will then be
advised that an airtime charge may be incurred (if we are running in the emulator then your local PC
Internet connection will be used instead). Confirm this action by selecting Yes.
The output window should show output similar to that shown in the following diagram.
5: Building a multimedia application with MIDP
Page: 9
Complementary Mobility
Building a multimedia application with MIDP
5: Building a multimedia application with MIDP
Page: 10
Download