|
How to read a resource from a JAR file |
|
|
The following Java ME code demonstrates a method of reading
resources from JAR file that is present on mobile handset.
An example where this kind of application may be used is
for example game developer wants to make an application for
voice recognition. Simple sound files like one, two etc.
are stored in a jared file and later can be accesed when
they are to be compared.
public class SoundDemo {
private byte[] fileNameBuf;
private byte[] buf=new byte[12*1000];
Player player;
public SoundDemo() {}
public static InputStream soundStreams;
// This method loads the sound
public void loadSounds() throws IOException {
{
// Reading resources of JAR file
InputStream in = getClass().getResourceAsStream(
"/sound.mid");
if (in != null) {
try {
int total = 0;
while (true) {
int numRead = in.read(buf,
total, buf.length-total);
if (numRead <= 0) {
break;
}
total += numRead;
}
byte[] soundBuf = new byte[total];
System.arraycopy(buf, 0, soundBuf, 0, total);
soundStreams = new ByteArrayInputStream(soundBuf);
// Create player object
player = Manager.createPlayer(soundStreams,
MIDI_FORMAT);
} catch (Exception e) {} finally {
in.close();
}
}
}
}
public void doPlay() throws Exception {
// Play the sound
player.start();
}
}
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.