J2ME Basics: Sounds Please..

[Techie's Tech Babble]
This is my first J2ME tutorial, so I'd like to appologize first for any unclarity on any other mistakes.

Playing a sound was one of my problems when I started developing J2ME games. How do I play a wav sound in J2ME? It was not long after I browse Nokia's API documentation that I found out how. In MIDP 1.0, playing sounds was mainly decided by the platform itself. Nokia seems to provide a good implementation with it's Sound class. Here is a sample of it's implementation:

Sound sound;
byte[] buffer = new byte[1644] ; //set buffer to the size of the wav sound's filesize
InputStream tune = this.getClass().getResourceAsStream("ding.wav"); //grabs the wav file from a jar file, through an InputStream
try {
tune.read(buffer, 0, buffer.length); // reads the data to a byte array
sound = new Sound(buffer, Sound.FORMAT_WAV); //create a new Sound object with buffer, with wav format
sound.init(buffer, Sound.FORMAT_WAV); //initialize the Sound object
sound.play(1); //plays the sound once, if set to 0 the sound with loop continuously
} catch (IOException e) {
e.printStackTrace();
}


It is a good practice to stop the sound first before playing it again, since it will reduce memory usage for one thing. This can be done simply by calling the sound.stop() method.

One of the problem with using Nokia's Sound class, is that you have to know the exact size of the wav file being played, since it will only accept byte arrays. Another problem is, of course, portability. This class' implementation is limited to Nokia phones only. However, things are changed with the presence of MIDP 2.0.

In MIDP 2.0, playing sounds -- and videos as well! -- are handled with a new class called Manager. The class resides under javax.microedition.media of the Mobile Media API (JSR-135). Manager class puts the implementations of sound manipulations above the native phone platform's, giving more portability. So if your phone platform or your SDK support MIDP 2.0, you should try code instead:

Player sound; //this is the Player object, used to represent the sound/video media
InputStream tune = this.getClass().getResourceAsStream("ding.wav"); //grabs the wav file from a jar file, through an InputStream
try{
sound = Manager.createPlayer(tune, "audio/x-wav"); // create a wave object from the InputStream of the type wav file
sound.prefecth(); //initialize the sound
sound.setMediaTime(0L); //set the media time, just to make sure it's accurate enough
sound.play(); //play the sound
} catch (Exception e) {
e.printStackTrace();
}

Creating a sound player through the Manager class does not require the knowledge of the media bytesize, providing easier changes in the media without having to alter the code once again. Also, it can accept streaming media by inserting the URI of the media:

sound = Manager.createPlayer("http://bhimz.blogspot.com/ding.wav");

It is up to you which implementation that you'll use, well actually, dependant to the phone platform you'll developed the game on. But as I am informed, there are many new phones released today with MIDP 2.0 embedded on them. Do experiment on them, and let me know if there are some differences or problems on specific types. ;)
Share on Google Plus

About BhimZ

Aria Bima Mahesa Putra (aka BhimZ) is an experienced software developer, with a lot of experience in various projects with different background. He is currently focused in mobile apps, and has been invested in game development ever since he was a kid in highschool. He founded BhimZ games, his one-man-army game studio as a mean to make his own brand of games during his spare times working as a software engineer in a multinational company. At leisure times he enjoys the company of his family, playing mobile games and watches Japanese tokusatsu and anime with his son.
    Blogger Comment
    Facebook Comment

1 comments:

Anonymous said...

schumer prakahar africahttp attempting homestead activitys benefit flexible catalog lwph wearable
lolikneri havaqatsu