Results 1 to 3 of 3
  1. #1
    schliz is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default Play wave file out of a jar file

    I am trying to play a wave file, which is in a not zipped jar file. I tried the following, it seems to work until byte[]audio = new byte[size], but the size I get is approximatly 2 GB and the wave file is only 130 KB where the wave file is located. And when I want to create the audio, I get a java.lang.OutOfMemoryError: Java heap space Exception.


    Java Code:
    JarFile jarFile=null;
    		try {
    			jarFile = new JarFile(strJarFileName);
    			ZipEntry soundFile = null;
    			soundFile = jarFile.getJarEntry(strWaveFileName);
    			
    	          if(soundFile!=null)System.err.println("sound file is not NULL here");
    	          InputStream is = null;
    	          is = jarFile.getInputStream(soundFile);
    	          BufferedInputStream bis = new BufferedInputStream(is);
    	          if(is!=null)System.err.println("is is NOT NULL");
    	         
    	          AudioInputStream audioInputStream=null;
    			try {
    				AudioFileFormat aff = AudioSystem.getAudioFileFormat(bis); 
    				audioInputStream = AudioSystem.getAudioInputStream(bis);
    				AudioFormat af = audioInputStream.getFormat();
    				int size = (int) (af.getFrameSize() * audioInputStream.getFrameLength());
    				byte[]audio = new byte[size];
    				DataLine.Info info = new DataLine.Info(Clip.class, af,size);
    				bis.read(audio,0,size);
    				Clip clip;
    				try {
    					clip = (Clip)AudioSystem.getLine(info);
    					clip.open(af, audio, 0, size);
    					clip.start();
    				} catch (LineUnavailableException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    Thank's for your help.
    Georg

  2. #2
    schliz is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default

    Hi!
    Can anybody please give me some advise. Thanks so much.
    Georg

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Is the jarfile in your classpath? If so, just use getResourceAsStream rather than futzuing around with the jarfile.

    You are also aware that "read" (i.e. bis.read) is not guaranteed to read the entire stream into the array in one go, right?

Similar Threads

  1. Play avi file in JMF
    By ramkumarm in forum AWT / Swing
    Replies: 0
    Last Post: 04-15-2009, 02:49 PM
  2. How to play Flash File inmy frame?
    By makpandian in forum AWT / Swing
    Replies: 1
    Last Post: 02-12-2009, 02:01 PM
  3. Replies: 3
    Last Post: 08-08-2008, 03:53 AM
  4. play vedio file and flash file in swing
    By rajasekharreddym in forum AWT / Swing
    Replies: 1
    Last Post: 07-30-2008, 09:22 PM
  5. how can I play an mp3 file in java?
    By iliana in forum Networking
    Replies: 1
    Last Post: 05-30-2008, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •