Search in sources :

Example 86 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project playn by threerings.

the class JavaAudio method createSound.

/**
 * Creates a sound instance from the audio data available via {@code in}.
 *
 * @param rsrc a resource via which the audio data can be read.
 * @param music if true, a custom {@link Clip} implementation will be used which can handle long
 * audio clips; if false, the default Java clip implementation is used which cannot handle long
 * audio clips.
 */
public JavaSound createSound(final JavaAssets.Resource rsrc, final boolean music) {
    final JavaSound sound = new JavaSound();
    ((JavaPlatform) platform).invokeAsync(new Runnable() {

        public void run() {
            try {
                AudioInputStream ais = rsrc.openAudioStream();
                Clip clip = AudioSystem.getClip();
                if (music) {
                    clip = new BigClip(clip);
                }
                AudioFormat baseFormat = ais.getFormat();
                if (baseFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
                    AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), // we have to force sample size to 16
                    16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), // big endian
                    false);
                    ais = AudioSystem.getAudioInputStream(decodedFormat, ais);
                }
                clip.open(ais);
                dispatchLoaded(sound, clip);
            } catch (Exception e) {
                dispatchLoadError(sound, e);
            }
        }
    });
    return sound;
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) Clip(javax.sound.sampled.Clip) AudioFormat(javax.sound.sampled.AudioFormat)

Example 87 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project d54 by mitrisdev.

the class AudioProcessor method openChannel.

/**
 * Open the audio channel so data can be captured.  This must be called once before frameUpdate or the
 * data accessors are called.
 */
public void openChannel() {
    try {
        line.open();
    } catch (LineUnavailableException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    line.start();
    line.drain();
    input = new AudioInputStream(line);
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) LineUnavailableException(javax.sound.sampled.LineUnavailableException)

Example 88 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project openblocks by mikaelhg.

the class SoundManager method loadSound.

public static Sound loadSound(String soundFileName) {
    final URL url = SoundManager.class.getResource(soundFileName);
    if (url == null) {
        System.out.println("Could not find resource " + soundFileName);
        return null;
    }
    AudioInputStream audioInputStream;
    try {
        audioInputStream = AudioSystem.getAudioInputStream(url);
    } catch (UnsupportedAudioFileException e) {
        return null;
    } catch (IOException e) {
        return null;
    }
    final AudioFormat format = audioInputStream.getFormat();
    final Clip clip;
    try {
        DataLine.Info info = new DataLine.Info(Clip.class, format);
        clip = (Clip) AudioSystem.getLine(info);
        clip.open(audioInputStream);
    } catch (LineUnavailableException e) {
        System.out.println("Sorry, sound is not available");
        return null;
    } catch (IOException e) {
        return null;
    }
    return new Sound(clip);
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) Clip(javax.sound.sampled.Clip) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) DataLine(javax.sound.sampled.DataLine) LineUnavailableException(javax.sound.sampled.LineUnavailableException) IOException(java.io.IOException) AudioFormat(javax.sound.sampled.AudioFormat) URL(java.net.URL)

Example 89 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project java-swing-tips by aterai.

the class LookAndFeelUtil method showMessageDialogAndPlayAudio.

public void showMessageDialogAndPlayAudio(Component p, String msg, String audioResource) {
    URL url = Thread.currentThread().getContextClassLoader().getResource(audioResource);
    if (url == null) {
        UIManager.getLookAndFeel().provideErrorFeedback(p);
        JOptionPane.showMessageDialog(p, audioResource + " not found");
        return;
    }
    try (AudioInputStream ss = AudioSystem.getAudioInputStream(url);
        Clip clip = (Clip) AudioSystem.getLine(new DataLine.Info(Clip.class, ss.getFormat()))) {
        SecondaryLoop loop = p.getToolkit().getSystemEventQueue().createSecondaryLoop();
        clip.addLineListener(e -> {
            LineEvent.Type t = e.getType();
            System.out.println(t);
            if (Objects.equals(t, LineEvent.Type.STOP) || Objects.equals(t, LineEvent.Type.CLOSE)) {
                loop.exit();
            }
        });
        clip.open(ss);
        clip.start();
        JOptionPane.showMessageDialog(p, msg);
        loop.enter();
    } catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
        ex.printStackTrace();
        UIManager.getLookAndFeel().provideErrorFeedback(p);
    }
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) Clip(javax.sound.sampled.Clip) LineEvent(javax.sound.sampled.LineEvent) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) LineUnavailableException(javax.sound.sampled.LineUnavailableException) IOException(java.io.IOException) URL(java.net.URL)

Example 90 with AudioInputStream

use of javax.sound.sampled.AudioInputStream in project java-swing-tips by aterai.

the class MainPanel method loadAndPlayAudio.

public void loadAndPlayAudio(String path) {
    try (AudioInputStream sound = AudioSystem.getAudioInputStream(getClass().getResource(path));
        Clip clip = (Clip) AudioSystem.getLine(new DataLine.Info(Clip.class, sound.getFormat()))) {
        SecondaryLoop loop = Toolkit.getDefaultToolkit().getSystemEventQueue().createSecondaryLoop();
        clip.addLineListener(e -> {
            LineEvent.Type t = e.getType();
            if (Objects.equals(t, LineEvent.Type.STOP) || Objects.equals(t, LineEvent.Type.CLOSE)) {
                loop.exit();
            }
        });
        clip.open(sound);
        clip.start();
        loop.enter();
    } catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
        ex.printStackTrace();
        Toolkit.getDefaultToolkit().beep();
    }
}
Also used : AudioInputStream(javax.sound.sampled.AudioInputStream) Clip(javax.sound.sampled.Clip) LineEvent(javax.sound.sampled.LineEvent) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) 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