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

Posted by mahmud ahsan on February 21, 2008 in Java/J2me | No Comments

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.

Related Posts

If you think this article kicked ass, subscribe to the RSS feed or follow me on Twitter! Share with your friends, or leave a comment below (or better still, do both!)

Leave a Reply