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.

mahmud ahsan

Computer programmer and hobbyist photographer from Bangladesh, lives in Malaysia. My [Business | Twitter | Linkedin | Instagram | Flickr | 500px]

You may also like

LEAVE A COMMENT