Saturday, March 19, 2022

Audio recording in Java

This Baeldung post explains clearly how to record from a microphone (source code available in github) . But it doesn't explain how to record from the headset. In my case I need to record not the mic but the sound played by a video being played. This trail explains very well the Sound API. This is how to print all the available Mixer devices on your System:
 
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Mixer;

public class DetectSystem {
    public static void main(String[] args) {
        System.out.println("DETECTING MIXERS");
        Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
        for (Mixer.Info mixerInfo : mixerInfos) {
            System.out.println(mixerInfo);
        }
    }
}
and on my system the output is:
DETECTING MIXERS
Primary Sound Driver, version Unknown Version
Headset Earphone (Plantronics C720-M), version Unknown Version
Speakers (Realtek High Definition Audio), version Unknown Version
Primary Sound Capture Driver, version Unknown Version
Headset Microphone (Plantronics, version Unknown Version
Microphone (Realtek High Defini, version Unknown Version
Port Headset Earphone (Plantronics C, version 0.83
Port Speakers (Realtek High Definiti, version 10.0
Port Headset Microphone (Plantronics, version 0.83
Port Microphone (Realtek High Defini, version 10.0
Here a nice collection of snippets. Here my github repo https://github.com/vernetto/screencapture.git