Here I’ll show, how to play a wav sound from a JAR file

To play a wav file we must include the Mobile Media API 1.1 (MMAPI 1.1) JSR 135.

At first I given the source code then explain it.

import javax.microedition.media.*; // This is the package you must include

try{
Player wavPlayer;
InputStream is = getClass().getResourceAsStream(“sound.wav”);
wavPlayer = Manager.createPlayer(is, “audio/X-wav”);
wavPlayer.prefetch();
wavPlayer.start();
}
catch(Exception o){
o.printStackTrace();
}

Player interface acts as a remote control to play all type of audio.
Manager.createPlayer() method is used to create a player. Here the first parameter is the source and second is the type of audio file.
wavPlayer.prefetch() method retrieving the audio and minimize latency.
wavPlayer.start() method start and play the audio.

There are some other methods necessary for controlling the player.
wavPlayer.setLoopCount(int number) . This method sets the number of times a sound is looped when play it.
If number is -1, then the loop will continue to infinite time.
wavPlayer.stop() method stop the playing audio.
wavPlayer.close() method close the player and release the resource.

Posted in J2ME. Tags: , , , , , , . Leave a Comment »

Leave a Reply