Play a multimedia file in J2ME Program (Audio/Video) using MMAPI

By Vikram Goyal Viewed: 31779 times Emailed: 208 times Printed: 216 times Bookmark and Share



The simplest MMAPI MIDlet that can be built allows you to easily play a multimedia file from within your MIDlet without worrying about controls, feature sets, or security architecture. If all you’re doing is adding some sampled audio (or any other media) in a game, MMAPI allows you to do so in two lines of code. Listing below shows this code within a complete MIDlet.

//A Simple MMAPI MIDlet

import javax.microedition.midlet.MIDlet;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
public class SimplePlayer extends MIDlet {

public void startApp() {

try {

Player player = Manager.createPlayer(getClass().getResourceAsStream("/media/audio/chapter3/baby.wav"),"audio/x-wav");
player.start();

} catch(Exception e) {
e.printStackTrace();
}

}

public void pauseApp() {

}

public void destroyApp(boolean unconditional) {

}

}

To keep things simple at this stage, the media file is played by creating an InputStream on a wav file, which is embedded in the MIDlet’s JAR. This media file is kept in the folder media/audio/chapter3 and is called baby.wav (which is the sound of a baby crying).

Of course, you don’t need to play an audio file only. You can substitute the wav file with a video file, provided the emulator supports the format of the video file. The video will not show anywhere, because this listing doesn’t provide a mechanism to show the video. You can substitute the wav file for a midi, tone, or any other supported audio format. The point is that playing multimedia files using the MMAPI is as simple as creating a Player instance using the Manager class and calling method start() on it.




Comments(8)


1. when iam trying to executing above program i am getting warning like this
java.lang.IllegalArgumentException
at javax.microedition.media.Manager.createPlayer(+11)
at SimplePlayer.startApp(+14)
at javax.microedition.midlet.MIDletProxy.startApp(+7)
at com.sun.midp.midlet.Scheduler.schedule(+270)
at com.sun.midp.main.Main.runLocalClass(+28)
at com.sun.midp.main.Main.main(+80)
what should i do?i am very new to j2me.please help me.

By: BHAGYALAXMI at 2008-10-11 06:45:57
2. pls send simple to hard j2me coding

By: Seenu at 2008-10-29 15:19:03
3. java.lang.IllegalArgumentException means usually that the argument was illegal/wrong/broken XD. So... my guess is that your file path is wrong
(the one you use to create the player)


By: Seink at 2008-11-12 11:03:00
4. me too got the same problem....
but m sure the file path is correct...



By: HEHE at 2008-11-19 01:49:04
5. I am not found a J2ME file for playing a audio player for my project.
please help me for my project.

By: S.M. ASHIQUER RAHMAN at 2008-11-22 22:33:43
6. I want to say, that you have to give complete code of programs
so that we learn something from your programs,
I am satisfied with your sitee.

By: Jasvir Yadav at 2009-09-05 07:23:15
7. very good helped me a lot

By: Swaran at 2009-12-30 09:51:36
8. I agree with BHAGYALAXMI. I faced the same problem. Plz give us a complete project (small) on j2me. We are very novice. But thank u Vikram Goyal for his article.

By: Alok at 2010-01-10 12:03:37

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:


Enter Code:
The Captcha image

Latest Tutorials

[2010-07-30]Code sample to Send SMS from a J2ME application.
[2009-05-29]Adding your own Application icon for your J2ME application (jar file)
[2008-08-18]Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
[2008-08-01]Datagrams in J2ME (UDP Programming sample)
[2008-08-01]Client Server in J2ME (Socket Programming sample)
[2008-08-01]Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
[2008-08-01]Using HTTP vs UDP vs Socket in J2ME
[2008-08-01]RMSCookieConnector - Using Cookies in J2ME
[2008-07-29]POST UTF-8 encoded data to the server in J2ME
[2008-07-10]lists, forms, choices, gauges, text fields, text boxes in J2ME
[2008-07-10]Using List to create a Menu and Menu items in J2ME
[2008-07-10]Using alerts and tickers in J2ME
[2008-07-07]J2ME Canvas sample to show games programming in J2ME
[2008-07-07]Timer and TimerTask example in J2ME
[2008-06-27]List of GPRS Access points for all countries

More Latest News

Most Viewed Articles (in last 30 days)
GUI components and menu based J2ME Applications.
Client Server in J2ME (Socket Programming sample)
Getting Started with J2ME
J2ME Canvas sample to show games programming in J2ME
RMS Basics in J2ME
Code sample to Send SMS from a J2ME application.
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
TextBox sample program in J2ME
'double buffering' Sample program in J2ME
Using List to create a Menu and Menu items in J2ME
Timer and TimerTask example in J2ME
Adding your own Application icon for your J2ME application (jar file)
What is J2ME?
Sample J2ME code that shows various functionality of RMS.
Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
Most Emailed Articles (in last 30 days)
What is J2ME?
Download a file over a network in J2ME midlet
How to load J2ME applications to the IDEN handsets
Getting Started with J2ME
Y.S. Sun Green Building Research Center
Sample J2ME code that shows various functionality of RMS.
Sample Java program shows how to Read a file over a network using J2ME midlet
Types of configurations in J2ME
'LINK.EXE' is not recognized as an internal or ext
RMS Basics in J2ME
GUI components and menu based J2ME Applications.
Your first J2ME program and a midlet lifecycle explained.
The MIDP Networking Model in J2ME
What is J2ME?
paint() sample program to draw a line in J2ME