Example usage for javax.sound.sampled AudioSystem getAudioInputStream

List of usage examples for javax.sound.sampled AudioSystem getAudioInputStream

Introduction

In this page you can find the example usage for javax.sound.sampled AudioSystem getAudioInputStream.

Prototype

public static AudioInputStream getAudioInputStream(final File file)
        throws UnsupportedAudioFileException, IOException 

Source Link

Document

Obtains an audio input stream from the provided File .

Usage

From source file:com.github.fcannizzaro.resourcer.Resourcer.java

/**
 * Create and AudioInputStream and update the field
 *
 * @throws IllegalAccessException//from  w  w w  .j a  v  a 2 s .c om
 */
private static void annotateAudio(Field field, Audio annotation, Object annotated)
        throws IllegalAccessException {
    try {
        String res = getPath(annotation.value(), AUDIO_DIR);
        field.set(annotated, AudioSystem.getAudioInputStream(new File(res)));
    } catch (IOException | UnsupportedAudioFileException e) {
        e.printStackTrace();
    }
}

From source file:org.sipfoundry.sipxconfig.site.common.AssetSelector.java

public static boolean isAcceptedAudioFormat(InputStream stream) {
    try {// w  ww  .  j  a  v a 2  s  . c  o  m
        InputStream testedStream = stream;
        // HACK: looks like in openjdk InputStream does not support mark/reset
        if (!stream.markSupported()) {
            // getAudioInputStream depends on mark reset do we wrap buffered input stream
            // around passed stream
            testedStream = new BufferedInputStream(stream);
        }
        AudioInputStream audio = AudioSystem.getAudioInputStream(new BufferedInputStream(testedStream));
        AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 8000, // sample
                // rate
                16, // bits per sample
                1, // channels
                2, // frame rate
                8000, // frame size
                false); // isBigEndian)
        return format.matches(audio.getFormat());
    } catch (IOException e) {
        LOG.warn("Uploaded file problems.", e);
    } catch (UnsupportedAudioFileException e) {
        LOG.info("Unsupported format", e);
    }
    return false;
}

From source file:Filter3dTest.java

/**
 * Opens a sound from a file./*from   w  w  w  .j a  va  2  s .  c  o m*/
 */
public SimpleSoundPlayer(String filename) {
    try {
        // open the audio input stream
        AudioInputStream stream = AudioSystem.getAudioInputStream(new File(filename));

        format = stream.getFormat();

        // get the audio samples
        samples = getSamples(stream);
    } catch (UnsupportedAudioFileException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:org.jtrfp.trcl.core.ResourceManager.java

public ResourceManager(final TR tr) {
    this.tr = tr;
    try {/*  w ww . j  a  v a 2 s .c  o m*/
        Class.forName("de.quippy.javamod.multimedia.mod.loader.tracker.ProTrackerMod");
        Class.forName("de.quippy.javamod.multimedia.mod.ModContainer"); // ModContainer uses the ModFactory!!
    } catch (Exception e) {
        tr.showStopper(e);
    }
    gpuResidentMODs = new CachedObjectFactory<String, GPUResidentMOD>() {
        @Override
        protected GPUResidentMOD generate(String key) {
            return new GPUResidentMOD(tr, getMOD(key));
        }//end generate(...)
    };
    soundTextures = new CachedObjectFactory<String, SoundTexture>() {
        @Override
        protected SoundTexture generate(String key) {
            try {
                final AudioInputStream ais = AudioSystem
                        .getAudioInputStream(getInputStreamFromResource("SOUND\\" + key));
                final FloatBuffer fb = ByteBuffer.allocateDirect((int) ais.getFrameLength() * 4)
                        .order(ByteOrder.nativeOrder()).asFloatBuffer();
                int value;
                while ((value = ais.read()) != -1) {
                    fb.put(((float) (value - 128)) / 128f);
                }
                fb.clear();
                return tr.soundSystem.get().newSoundTexture(fb, (int) ais.getFormat().getFrameRate());
            } catch (Exception e) {
                tr.showStopper(e);
                return null;
            }
        }
    };

    setupPODListeners();
}

From source file:com.limegroup.gnutella.gui.mp3.BasicPlayer.java

/**
 * Inits Audio ressources from file./*from   ww w  . jav a  2s  . co m*/
 */
private void initAudioInputStream(File file) throws UnsupportedAudioFileException, IOException {
    _file = file;
    m_audioInputStream = AudioSystem.getAudioInputStream(file);
    m_audioFileFormat = AudioSystem.getAudioFileFormat(file);
}

From source file:com.limegroup.gnutella.gui.mp3.BasicPlayer.java

/**
 * Inits Audio ressources from URL./*from  w  w w . j av  a  2s  .c o  m*/
 */
private void initAudioInputStream(URL url) throws UnsupportedAudioFileException, IOException {
    m_audioInputStream = AudioSystem.getAudioInputStream(url);
    m_audioFileFormat = AudioSystem.getAudioFileFormat(url);
}

From source file:com.github.woz_dialog.ros_woz_dialog_project.TTSHTTPClient.java

public static AudioInputStream getAudioStream(byte[] byteArray) {
    try {/*from   w  w  w.j av a2 s .  co m*/
        try {
            ByteArrayInputStream byteStream = new ByteArrayInputStream(byteArray);
            return AudioSystem.getAudioInputStream(byteStream);
        } catch (UnsupportedAudioFileException e) {
            byteArray = addWavHeader(byteArray);
            ByteArrayInputStream byteStream = new ByteArrayInputStream(byteArray);
            return AudioSystem.getAudioInputStream(byteStream);
        }
    } catch (IOException | UnsupportedAudioFileException e) {
        throw new RuntimeException("cannot convert bytes to audio stream: " + e);
    }
}

From source file:de.tor.tribes.ui.windows.ClockFrame.java

public synchronized void playSound(String pSound) {
    Clip clip = null;/* w ww.j  a v  a  2s  .c o m*/
    AudioClip ac = null;
    try {
        if (org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS) {
            clip = AudioSystem.getClip();
            BufferedInputStream bin = new BufferedInputStream(
                    ClockFrame.class.getResourceAsStream("/res/" + pSound + ".wav"));
            AudioInputStream inputStream = AudioSystem.getAudioInputStream(bin);
            clip.open(inputStream);
            clip.start();
        } else {
            ac = Applet.newAudioClip(ClockFrame.class.getResource("/res/" + pSound + ".wav"));
            ac.play();
        }
    } catch (Exception e) {
        logger.error("Failed to play sound", e);
    }
    try {
        Thread.sleep(2500);
    } catch (Exception ignored) {
    }

    try {
        if (clip != null) {
            clip.stop();
            clip.flush();
            clip = null;
        }

        if (ac != null) {
            ac.stop();
            ac = null;
        }
    } catch (Exception ignored) {
    }
}

From source file:com.player.BasicMP3Player.java

/**
 * Inits Audio ressources from file.//from  w  w w .  j  a  v a2  s . c  o m
 */
private void initAudioInputStream(File file) throws UnsupportedAudioFileException, IOException {
    m_audioInputStream = AudioSystem.getAudioInputStream(file);
    m_audioFileFormat = AudioSystem.getAudioFileFormat(file);
}

From source file:com.marginallyclever.makelangelo.MainGUI.java

private void PlaySound(String url) {
    if (url.isEmpty())
        return;/*from   ww  w  . ja  v  a2  s  .  c om*/

    try {
        Clip clip = AudioSystem.getClip();
        BufferedInputStream x = new BufferedInputStream(new FileInputStream(url));
        AudioInputStream inputStream = AudioSystem.getAudioInputStream(x);
        clip.open(inputStream);
        clip.start();
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println(e.getMessage());
    }
}