Example usage for javax.sound.sampled AudioInputStream AudioInputStream

List of usage examples for javax.sound.sampled AudioInputStream AudioInputStream

Introduction

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

Prototype

public AudioInputStream(TargetDataLine line) 

Source Link

Document

Constructs an audio input stream that reads its data from the target data line indicated.

Usage

From source file:org.snitko.app.record.SoundRecorder.java

public AudioInputStream record(long durationInSeconds) throws LineUnavailableException {
    Mixer.Info[] mixers = AudioSystem.getMixerInfo();
    for (Mixer.Info mixer : mixers) {
        System.out.printf("mixer name/desc/vendor/version: %s, %s, %s, %s\n", mixer.getName(),
                mixer.getDescription(), mixer.getVendor(), mixer.getVersion());
    }//www . j a  v  a  2s  .  c o  m

    AudioFileFormat.Type[] types = AudioSystem.getAudioFileTypes();
    for (AudioFileFormat.Type type : types) {
        System.out.println("audio types are: " + type.toString());
    }

    AudioFormat audioFormat = getAudioFormat();

    // the targetDataLine from which audio data is captured
    TargetDataLine targetDataLine = AudioSystem.getTargetDataLine(audioFormat);
    System.out.println("targetDataLine: " + targetDataLine + "\n\tlineInfo" + targetDataLine.getLineInfo()
            + "\n\tformat: " + targetDataLine.getFormat());

    // checks if system supports the data targetDataLine
    if (!AudioSystem.isLineSupported(targetDataLine.getLineInfo())) {
        System.out.println("Line not supported");
        System.exit(0);
    }

    targetDataLine.addLineListener(new TimedLineListener(targetDataLine, durationInSeconds));
    targetDataLine.open(audioFormat);
    targetDataLine.start(); // start capturing

    System.out.println("Start capturing...");
    AudioInputStream audioInputStream = new AudioInputStream(targetDataLine);
    System.out.println("Start recording...");
    return audioInputStream;
}