import java.io.InputStream; import java.io.FileInputStream; import sun.audio.AudioStream; import sun.audio.AudioData; import sun.audio.ContinuousAudioDataStream; import sun.audio.AudioPlayer; public class SoundLoopWithApplication1 { public static void main(String[] args) { // Note that instead of using a "try-catch" clause to trap for (and // handle) any errors that might occur while reading or playing the // sound file, "throws Exception" could be used above to have errors // propagate back to the caller; see "SoundPlayWithApplication.java" // for an example of that approach to error trapping; also see // "SoundLoopWithApplication2.java" for a much-condensed version of // this program that also uses the "throws Exception" clause try { // Open an 'InputStream' object to the sound file InputStream file = new FileInputStream("data_files/sound.wav"); // Create an 'AudioStream' object from the 'InputStream' object AudioStream sound = new AudioStream(file); // Create an AudioData source from the 'AudioStream' object AudioData data = sound.getData(); // Create a ContinuousAudioDataStream from the AudioData source ContinuousAudioDataStream soundRepeat = new ContinuousAudioDataStream(data); // Use the static class member "player" from the // 'AudioPlayer' class to play the sound clip AudioPlayer.player.start(soundRepeat); } catch (Exception error) { // Do nothing...just ignore the error } } }