/** Creates an AudioInputStream from a sound from an input stream */ public AudioInputStream getAudioInputStream(InputStream is) { try { if (!is.markSupported()) { is = new BufferedInputStream(is); } // open the source stream AudioInputStream source = AudioSystem.getAudioInputStream(is); // convert to playback format return AudioSystem.getAudioInputStream(playbackFormat, source); } catch (UnsupportedAudioFileException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } catch (IllegalArgumentException ex) { ex.printStackTrace(); } return null; }
public static void Play() { try { // Open an audio input stream. File soundFile = new File("foo.wav"); AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile); // Get a sound clip resource. Clip clip = AudioSystem.getClip(); // Open audio clip and load samples from the audio input stream. clip.open(audioIn); clip.start(); } catch (UnsupportedAudioFileException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (LineUnavailableException e) { e.printStackTrace(); } }
// Constructor to construct each element of the enum with its own sound file. SoundEffect(String soundFileName) { try { // Use URL (instead of File) to read from disk and JAR. URL url = this.getClass().getClassLoader().getResource(soundFileName); // Set up an audio input stream piped from the sound file. AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url); // Get a clip resource. clip = AudioSystem.getClip(); // Open audio clip and load samples from the audio input stream. clip.open(audioInputStream); } catch (UnsupportedAudioFileException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (LineUnavailableException e) { e.printStackTrace(); } }