OutOfMemoryError with wav file bigger than 30kb
OS: Ubuntu 7.04
Java version 1.5.0_12
I'm trying to run the simplest of examples here, just playing a wav file:
import java.io.*;
import java.util.*;
import javax.sound.sampled.*;
publicclass Main{
publicstaticvoid main(String[] argv){
try{
// From file
AudioInputStream stream = AudioSystem.getAudioInputStream(new File("/home/iii/iii1.wav"));
// At present, ALAW and ULAW encodings must be converted
// to PCM_SIGNED before it can be played
AudioFormat format = stream.getFormat();
if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED){
format =new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(),
format.getSampleSizeInBits()*2,
format.getChannels(),
format.getFrameSize()*2,
format.getFrameRate(),
true);// big endian
stream = AudioSystem.getAudioInputStream(format, stream);
}
// Create the clip
DataLine.Info info =new DataLine.Info(
Clip.class, stream.getFormat(), ((int)stream.getFrameLength()*format.getFrameSize()));
Clip clip = (Clip) AudioSystem.getLine(info);
// This method does not return until the audio file is completely loaded
clip.open(stream);
// Start playing
clip.start();
Thread.sleep( 5000 );
}catch(Exception e){
e.printStackTrace();
}
}
}
And all this works fine if the wav file is smaller than around 30KB. Otherwise I get:
Exception in thread"main" java.lang.OutOfMemoryError: Java heap space
at com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1127)
at Main.main(Main.java:39)
(that is the clip.open(stream);
line)
Unfortunately very few wav files are small enough for this code to have any practical value. For the sake of the experiment I run it with say -Xms256m -Xmx512m (the machine has 2GB RAM) which are ridiculously high for such a simple program but there is no difference. The same code on a Windows XP box plays a bit bigger files (around 70KB) but behaves oddly with .wav files bigger than say 100KB. It doesn't throw OutOfMemory or any other exceptions but just won't play them.
Appreciate your help! If the above code isn't good can you point me to or paste some java example which will hopefully use javax.sound.sampled and be able to open wav files of size at least 200KB.
Thank you...

