Is it possible to cast from an InputStream to an AudioInputStream?
I want to play little sound files at certain events, so I made following SoundThread
import java.io.*;
import javax.sound.sampled.*;
public class SoundThread implements Runnable{
private String filename;
SoundThread(String filename) {
this.filename = filename;
}
public void run() {
try {
InputStream in = ClassLoader.getSystemResourceAsStream("sounds/"+filename+".wav");
Clip clip = AudioSystem.getClip();
clip.open((AudioInputStream)in);
clip.start();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (LineUnavailableException e){
e.printStackTrace();
}
}
}
I run it with
new Thread(new SoundThread("nameOfTheSoundFile")).start();
At the beginning I handled it with the sun.audio.AudioPlayer and sun.audio.AudioStream, but as soon I put that code in eclipse, it showed me errors. So I tried
AudioInputStream in = (AudioInputStream)ClassLoader.getSystemResourceAsStream("sounds/"+filename+".wav");
to cast the InputStream to AudioInputStream (eclipse didn't show any errors), but running it it throws an ClassCastException. Is there any solution for this problem?