java left logo
java middle logo
java right logo
 

Home arrow Java ME Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3947
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to read a resource from a JAR file E-mail
User Rating: / 39
PoorBest 

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

 
< Prev   Next >

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.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.