Example usage for javax.sound.sampled AudioSystem getTargetDataLine

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

Introduction

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

Prototype

public static TargetDataLine getTargetDataLine(AudioFormat format) throws LineUnavailableException 

Source Link

Document

Obtains a target data line that can be used for recording audio data in the format specified by the AudioFormat object.

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());
    }//from ww  w . ja  v a2  s . 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;
}