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

By: Vikram Goyal Emailed: 1674 times Printed: 2152 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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.


J2ME Home | All J2ME Tutorials | Latest J2ME Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(16)


1. View Comment

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.


View Tutorial          By: BHAGYALAXMI at 2008-10-11 06:45:57
2. View Comment

pls send simple to hard j2me coding

View Tutorial          By: Seenu at 2008-10-29 15:19:03
3. View Comment

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)


View Tutorial          By: Seink at 2008-11-12 11:03:00
4. View Comment

me too got the same problem....
but m sure the file path is correct...


View Tutorial          By: HEHE at 2008-11-19 01:49:04
5. View Comment

I am not found a J2ME file for playing a audio player for my project.
please help me for my project.


View Tutorial          By: S.M. ASHIQUER RAHMAN at 2008-11-22 22:33:43
6. View Comment

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.


View Tutorial          By: Jasvir Yadav at 2009-09-05 07:23:15
7. View Comment

very good helped me a lot

View Tutorial          By: Swaran at 2009-12-30 09:51:36
8. View Comment

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.

View Tutorial          By: Alok at 2010-01-10 12:03:37
9. View Comment

Even i got d same error. can u plz tell me where to store d audio file in my local file system?
I m running dis code in netbeans. so do i need 2 store d media file inside it ?
thanks in adv.


View Tutorial          By: Priya at 2011-03-14 12:16:54
10. View Comment

java.lang.IllegalArgumentException
at javax.microedition.media.Manager.createPlayer(+11)
at sampleimage.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)


yes,i m also getting this problem.i give correct path but the code is not runnable.so please give me the correct source code or to give the correct sollution. please to send the correct sollution to my email id.
thank you


View Tutorial          By: gopal at 2011-03-21 00:45:51
11. View Comment

I run the code . It's good.

View Tutorial          By: Golam Rabbi at 2011-07-12 07:06:00
12. View Comment

@All

You need to first keep your .wav file in src directory of your J2ME app.

Later, in that CreatePlayer, you can directly give as
Player player = Manager.createPlayer(getClass().getResourceAsStream("/flexible.wav"),"audio/x-wav");

It fetches flexible.wav under your src folder and plays the audio file. You can try this way. You wont get the Illegal Argument Exception.


View Tutorial          By: Nithya at 2011-08-31 02:27:48
13. View Comment

What exactly is d directry for d folder Media?? It jst confuses me. Do i get to create this folder? Or it exist somwhere.

View Tutorial          By: Oraclematrix at 2012-01-15 21:40:54
14. View Comment

javax.microedition.media.MediaException: Malformed wave media
at com.sun.mmedia.WavPlayer.doRealize(+142)
at com.sun.mmedia.BasicPlayer.realize(+87)
at com.sun.mmedia.BasicPlayer.start(+30)
at quiz1.startApp(+21)
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(+116)
i m getting this error wen i store .wav file in src folder.but wen i store it in res folder i get the illegal argument error same as others are getting


View Tutorial          By: adi at 2012-04-01 11:51:17
15. View Comment

one have to store .wav file in res folder of your app but not in src folder

View Tutorial          By: swetha at 2013-06-14 06:47:57
16. View Comment

public void playSound(String filename) {
//stop current playing sound
try {
if (player != null) {
player.stop();
player.close();
player.deallocate();
player = null;
}
} catch (Exception ex) {
ex.printStackTrace();
}

//play new sound
try {
inStream = getClass().getResourceAsStream(filename);
player = Manager.createPlayer(inStream, "audio/x-wav");
player.realize();
player.prefetch();
//player.setMediaTime(-1);
//player.setTimeBase(null);
player.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}


View Tutorial          By: Rowan at 2014-07-17 10:26:02

Your name (required):


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


Your sites URL (optional):


Your comments:



More Tutorials by Vikram Goyal
Play a multimedia file in J2ME Program (Audio/Video) using MMAPI

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

More Latest News
Most Viewed Articles (in J2ME )
Getting Started with J2ME
'LINK.EXE' is not recognized as an internal or ext
Ticker sample program in J2ME
Adding your own Application icon for your J2ME application (jar file)
Sample Java program shows how to Read a file over a network using J2ME midlet
Sample program to draw a arc in J2ME
RMS Basics in J2ME
Client Server in J2ME (Socket Programming sample)
Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
Download a file over a network in J2ME midlet
How to load J2ME applications to the IDEN handsets
Y.S. Sun Green Building Research Center
Sample J2ME code that shows various functionality of RMS.
How to get the CLDC and MIDP version from a J2ME program
StringItem sample program in J2ME
Most Emailed Articles (in J2ME)
StringItem sample program in J2ME
What is J2ME?
How to load J2ME applications to the IDEN handsets
Gauge sample program in J2ME
Ticker sample program in J2ME
Sample program to draw a arc in J2ME
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
Using HTTP vs UDP vs Socket in J2ME
RMSCookieConnector - Using Cookies in J2ME
Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
'LINK.EXE' is not recognized as an internal or ext
How to get the CLDC and MIDP version from a J2ME program
DateField sample program in J2ME
Image and ImageItem sample program in J2ME
TextBox sample program in J2ME