The play
method below is from a class which, upon instantiation, reads a .wav file into a byte array called data
, and stores the sound format in an
Amplifying on @Anrew's and @Phil's answers, there's an outline of obtaining a Clip
and playing it asynchronously below. There's a complete example here.
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(...);
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
...
// Play the sound in a separate thread.
private void playSound() {
Runnable soundPlayer = new Runnable() {
@Override
public void run() {
try {
clip.setMicrosecondPosition(0);
clip.start();
} catch (Exception e) {
e.printStackTrace();
}
}
};
new Thread(soundPlayer).start();
}