Search in sources :

Example 71 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project SimpleAsteroids by ljialin.

the class SoundManager method getClip.

public static Clip getClip(String filename) {
    Clip clip = null;
    try {
        clip = AudioSystem.getClip();
        AudioInputStream sample = AudioSystem.getAudioInputStream(new File(path + filename + ".wav"));
        clip.open(sample);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return clip;
}
Also used : Clip(javax.sound.sampled.Clip) AudioInputStream(javax.sound.sampled.AudioInputStream) File(java.io.File)

Example 72 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project yamcs-studio by yamcs.

the class SoundSystem method beep.

/**
 * Plays a beep, only if there is not currently a previous beep playing.
 */
public static synchronized void beep() {
    try {
        if (beepClip != null) {
            return;
        }
        beepClip = AudioSystem.getClip();
        InputStream in = SoundSystem.class.getResourceAsStream(BEEP_SOUND);
        AudioInputStream audioIn = AudioSystem.getAudioInputStream(new BufferedInputStream(in));
        beepClip.open(audioIn);
        beepClip.start();
        // explicitly stop the clip when sound has stopped
        beepClip.addLineListener(new LineListener() {

            @Override
            public void update(LineEvent event) {
                if (event.getType() == LineEvent.Type.STOP) {
                    event.getLine().close();
                    beepClip = null;
                }
            }
        });
    } catch (Exception e) {
        System.out.println(e);
        log.log(Level.WARNING, "Error playing beep sound", e);
    }
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) LineEvent(javax.sound.sampled.LineEvent) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) InputStream(java.io.InputStream) LineListener(javax.sound.sampled.LineListener)

Example 73 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project algs4 by kevin-wayne.

the class StdAudio method readByte.

// return data as a byte array
private static byte[] readByte(String filename) {
    byte[] data = null;
    AudioInputStream ais = null;
    try {
        // try to read from file
        File file = new File(filename);
        if (file.exists()) {
            ais = AudioSystem.getAudioInputStream(file);
            int bytesToRead = ais.available();
            data = new byte[bytesToRead];
            int bytesRead = ais.read(data);
            if (bytesToRead != bytesRead)
                throw new IllegalStateException("read only " + bytesRead + " of " + bytesToRead + " bytes");
        } else // try to read from URL
        {
            URL url = StdAudio.class.getResource(filename);
            ais = AudioSystem.getAudioInputStream(url);
            int bytesToRead = ais.available();
            data = new byte[bytesToRead];
            int bytesRead = ais.read(data);
            if (bytesToRead != bytesRead)
                throw new IllegalStateException("read only " + bytesRead + " of " + bytesToRead + " bytes");
        }
    } catch (IOException e) {
        throw new IllegalArgumentException("could not read '" + filename + "'", e);
    } catch (UnsupportedAudioFileException e) {
        throw new IllegalArgumentException("unsupported audio format: '" + filename + "'", e);
    }
    return data;
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL)

Example 74 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project algs4 by kevin-wayne.

the class StdAudio method stream.

// https://www3.ntu.edu.sg/home/ehchua/programming/java/J8c_PlayingSound.html
// play a wav or aif file
// javax.sound.sampled.Clip fails for long clips (on some systems)
private static void stream(String filename) {
    SourceDataLine line = null;
    // 4K buffer
    int BUFFER_SIZE = 4096;
    try {
        InputStream is = StdAudio.class.getResourceAsStream(filename);
        AudioInputStream ais = AudioSystem.getAudioInputStream(is);
        AudioFormat audioFormat = ais.getFormat();
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
        line = (SourceDataLine) AudioSystem.getLine(info);
        line.open(audioFormat);
        line.start();
        byte[] samples = new byte[BUFFER_SIZE];
        int count = 0;
        while ((count = ais.read(samples, 0, BUFFER_SIZE)) != -1) {
            line.write(samples, 0, count);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (UnsupportedAudioFileException e) {
        e.printStackTrace();
    } catch (LineUnavailableException e) {
        e.printStackTrace();
    } finally {
        if (line != null) {
            line.drain();
            line.close();
        }
    }
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) ByteArrayInputStream(java.io.ByteArrayInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) InputStream(java.io.InputStream) DataLine(javax.sound.sampled.DataLine) SourceDataLine(javax.sound.sampled.SourceDataLine) LineUnavailableException(javax.sound.sampled.LineUnavailableException) SourceDataLine(javax.sound.sampled.SourceDataLine) IOException(java.io.IOException) AudioFormat(javax.sound.sampled.AudioFormat)

Example 75 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project algs4 by kevin-wayne.

the class StdAudio method loop.

/**
 * Loops an audio file (in .wav, .mid, or .au format) in a background thread.
 *
 * @param filename the name of the audio file
 * @throws IllegalArgumentException if {@code filename} is {@code null}
 */
public static synchronized void loop(String filename) {
    if (filename == null)
        throw new IllegalArgumentException();
    // code adapted from: http://stackoverflow.com/questions/26305/how-can-i-play-sound-in-java
    try {
        Clip clip = AudioSystem.getClip();
        InputStream is = StdAudio.class.getResourceAsStream(filename);
        AudioInputStream ais = AudioSystem.getAudioInputStream(is);
        clip.open(ais);
        clip.loop(Clip.LOOP_CONTINUOUSLY);
    } catch (UnsupportedAudioFileException e) {
        throw new IllegalArgumentException("unsupported audio format: '" + filename + "'", e);
    } catch (LineUnavailableException e) {
        throw new IllegalArgumentException("could not play '" + filename + "'", e);
    } catch (IOException e) {
        throw new IllegalArgumentException("could not play '" + filename + "'", e);
    }
}
Also used : Clip(javax.sound.sampled.Clip) AudioClip(java.applet.AudioClip) AudioInputStream(javax.sound.sampled.AudioInputStream) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) ByteArrayInputStream(java.io.ByteArrayInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) InputStream(java.io.InputStream) LineUnavailableException(javax.sound.sampled.LineUnavailableException) IOException(java.io.IOException)

Aggregations

AudioInputStream (javax.sound.sampled.AudioInputStream)92 IOException (java.io.IOException)44 AudioFormat (javax.sound.sampled.AudioFormat)41 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)27 InputStream (java.io.InputStream)24 ByteArrayInputStream (java.io.ByteArrayInputStream)19 LineUnavailableException (javax.sound.sampled.LineUnavailableException)19 File (java.io.File)18 SourceDataLine (javax.sound.sampled.SourceDataLine)14 Clip (javax.sound.sampled.Clip)13 AudioFileFormat (javax.sound.sampled.AudioFileFormat)12 DataLine (javax.sound.sampled.DataLine)12 FileInputStream (java.io.FileInputStream)11 BufferedInputStream (java.io.BufferedInputStream)10 URL (java.net.URL)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 DataInputStream (java.io.DataInputStream)5 SequenceInputStream (java.io.SequenceInputStream)5 LineEvent (javax.sound.sampled.LineEvent)5 AudioSynthesizer (com.sun.media.sound.AudioSynthesizer)4