I have a very simple class that can play a sound file with the following code:
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class Sound{
private Clip sound;
public Sound(String location){
try{
sound = AudioSystem.getClip();
File file = new File(location);
sound.open(AudioSystem.getAudioInputStream(file));
}
catch(IOException | LineUnavailableException | UnsupportedAudioFileException error){
System.out.println(error);
}
}
public void play(){
sound.start();
}
}
However, when I create an instance of this class and call the play function on it I don't get any sound. I hear a pop when the sound starts and when it ends but not the actual file. Also I don't get any errors of any sort.
What am I doing wrong?
Try something like:
File soundFile = new File( "something.wav" );
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream( soundFile );
clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();//This plays the audio
You might have to use AudioSystem.getClip()
after loading the audio stream.