So i was playing around with playing audio a bit and i found a question on this site which i found some code on which i slightly adapted that answered playing audio but when i tried to use the code i got a java.lang.OutOfMemoryError.
Then i tried starting it with two gigabytes of ram and it still gave me the error, I honestly have no idea what's going on my code is as follows:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
public class PlaySoundTest {
public static void main(String[] args){
playSound("audio.wav");
}
public static synchronized void playSound(final String url){
new Thread(new Runnable(){
public void run(){
try {
Clip clip = AudioSystem.getClip();
InputStream bufferedIn = new BufferedInputStream(new FileInputStream(new File(url)));
AudioInputStream audioSrc = AudioSystem.getAudioInputStream(bufferedIn);
clip.open(audioSrc);
clip.start();
}catch (Exception e){
System.err.println(e.getMessage());
}
}
}).start();
}
}
And the entire stacktrace is this
Exception in thread "Thread-0" java.lang.OutOfMemoryError: Java heap space
at com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1131)
at PlaySoundTest$1.run(PlaySoundTest.java:24)
at java.lang.Thread.run(Thread.java:722)
This is really confusing me because to me this code looks fine. Just to clarify the size of audio.wav is 1.5 MB so with 2 gigabytes allocated this really should not be an issue.
I'm thinking maybe a Memory leak somewhere but i couldn't think why.
I would be happy if anyone could shed some light on this.