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

By: Vikram Goyal Viewed: 153283 times  Printer Friendly Format    


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.



Most Viewed Articles (in J2ME )

Latest Articles (in J2ME)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. when iam trying to executing above program i am ge
View Tutorial          By: BHAGYALAXMI at 2008-10-11 06:45:57

2. pls send simple to hard j2me coding
View Tutorial          By: Seenu at 2008-10-29 15:19:03

3. java.lang.IllegalArgumentException means usually t
View Tutorial          By: Seink at 2008-11-12 11:03:00

4. me too got the same problem....
but m sure

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

5. me too got the same problem....
but m sure

View Tutorial          By: HEHE at 2008-11-19 01:50:15

6. I am not found a J2ME file for playing a audio pla
View Tutorial          By: S.M. ASHIQUER RAHMAN at 2008-11-22 22:33:43

7. I want to say, that you have to give complete code
View Tutorial          By: Jasvir Yadav at 2009-09-05 07:23:15

8. You are totally learning yourself
The code

View Tutorial          By: Narayan at 2009-09-14 03:28:35

9. very good helped me a lot
View Tutorial          By: Swaran at 2009-12-30 09:51:36

10. I agree with BHAGYALAXMI. I faced the same problem
View Tutorial          By: Alok at 2010-01-10 12:03:37

11. Even i got d same error. can u plz tell me where t
View Tutorial          By: Priya at 2011-03-14 12:16:54

12. java.lang.IllegalArgumentException
at java

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

13. I run the code . It's good.
View Tutorial          By: Golam Rabbi at 2011-07-12 07:06:00

14. @All

You need to first keep your .w

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

15. What exactly is d directry for d folder Media?? It
View Tutorial          By: Oraclematrix at 2012-01-15 21:40:54

16. javax.microedition.media.MediaException: Malformed
View Tutorial          By: adi at 2012-04-01 11:51:17

17. Put *.wav at where store java file and nnsert thes
View Tutorial          By: BAE at 2012-09-18 12:26:55

18. one have to store .wav file in res folder of your
View Tutorial          By: swetha at 2013-06-14 06:47:57

19. public void playSound(String filename) {

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