B
B
Boris Onofrey2014-05-06 22:08:33
Java
Boris Onofrey, 2014-05-06 22:08:33

Java. How to capture sound from audio output?

You need to get sound from the audio output.
To get sound from the microphone, I use the following code:

AudioFormat audioFormat = getAudioFormat();
DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
TargetDataLine targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
targetDataLine.open(audioFormat);
targetDataLine.start();

//Ну и читаем
targetDataLine.read(tempBuffer, 0, tempBuffer.length);

The problem is that TargetDataLine allows you to capture sound only from a microphone, and SourceDataLine only serves to output sound.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Khabarov, 2014-05-08
@RedCreepster

There is a lot of useful information on the links:
Java Sound Resources: FAQ: Audio Programming
Wired for sound
Java Microphone Selection and Level Monitoring
1. When recording from a device, you cannot select the input port directly. The input port goes to the mixer. You can record from the mixer output (TargetDataLine).
But, if there are several input devices (microphone, line, CD), then you can put "Mute" on unnecessary inputs (via the Control interface), and get a recording from the desired source at the mixer output. Similarly, through Control, you can control the volume of the selected source. Controls need to be received after Line.open().
2. The input is activated only if an external device is connected to it. At least that's what happens with the Realtek sound cards I've seen. And, in this case, it can be "seen" via AudioSystem.getSourceLineInfo(<port type>). For some reason, the microphone input is usually active, perhaps because it is displayed on the front panel of the system unit.
3. In the Realtek HD manager, in the advanced settings, the default recording device is "combined", i.e. all recording devices are displayed as one. You need to select the option "Split input jacks ...."

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question