import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; public class SoundPlayLoopStopWithWAV { public static void main(String[] args) throws Exception { new SoundPlayLoopStopWithWAV(); } public SoundPlayLoopStopWithWAV() throws Exception { // The 'AudioSystem' class requires exception trapping (by using // either a 'throws Exception' or 'try-catch' statement) Clip audioClip = AudioSystem.getClip(); // The .WAV file bit depth cannot be greater than 16, but there // appears to be no limit on the size of the files; with Eclipse, // to access sound files using the approach below, the files must // be located in the 'bin' folder for the program being run; the // benefit of this approach is that no code changes are needed // when putting .CLASS and audio files into a single .JAR file AudioInputStream inputStream = AudioSystem.getAudioInputStream(getClass().getResource("data_files/lastdate.wav")); // Load the audio from from disk audioClip.open(inputStream); // Start the audio file playing audioClip.start(); // The line below exists solely to keep this demo program running; // otherwise, this program will end and the sound will stop playing while (audioClip.isOpen()); // Have the audio file loop (repeat) continuously; replace the value // in parentheses with a positive integer to have the sound loop a // specific number of times // audioClip.loop(Clip.LOOP_CONTINUOUSLY); // Stop the audio file from playing // audioClip.close(); } }