I have implemented a simple Java sound player (.wav) that works well, except that after playing 50-60 sounds it just stops working.
The code is similar to Playing .mp3 and .wav in Java?, but I will post mine here anyway:
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
public class NewWavPlayer {
private URL url;
private AudioInputStream audio;
public NewWavPlayer(URL url) {
this.url = url;
}
public void play() {
try
{
InputStream audioSrc = url.openStream();
InputStream bufferedIn = new BufferedInputStream(audioSrc);
audio = AudioSystem.getAudioInputStream(bufferedIn);
Clip clip = AudioSystem.getClip();
clip.open(audio);
clip.start();
} catch(Exception e) {
System.err.println(e);
}
}
}
The error that I get after a while is:
javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 44100.0 Hz, 16 bit, mono, 2 bytes/frame, little-endian not supported.
or
javax.sound.sampled.LineUnavailableException: line with format PCM_UNSIGNED 22050.0 Hz, 8 bit, mono, 1 bytes/frame, not supported.
But it does not make sense, because the same sound was played before (or other sounds that already played correctly). I'm running it on a Linux Ubuntu 15.04, and other users experienced the same with other SOs.
I have tried reimplementing it with threads, but I got the same error. I would like to know which steps should I take next.