Hijack audio with Java?

I am trying to modify the code found at the bottom of this page to capture system sound using Java. Here is the part that I modified in captureAudio ():

Mixer mixer = AudioSystem.getMixer(mixerInfo[0]); // "Java Sound Audio Engine" final TargetDataLine line = (TargetDataLine) mixer.getLine(info); 

Now, when I run this code, it produces the following:

 Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Line unsupported: interface TargetDataLine supporting format PCM_SIGNED 44100.0 Hz, 16 bit, mono, 2 bytes/frame, big-endian 

I tried to change my format so that it corresponded to the required format, but the exception does not go and nothing is written. What am I doing wrong?

+4
source share
1 answer

Try the following

 TargetDataLine line; DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); // format is an AudioFormat object if (!AudioSystem.isLineSupported(info)) { // Handle the error. } // Obtain and open the line. try { line = (TargetDataLine) AudioSystem.getLine(info); line.open(format); } catch (LineUnavailableException ex) { // Handle the error. //... } 

This is taken from http://docs.oracle.com/javase/tutorial/sound/accessing.html

To create an AudioFormat, use

new AudioFormat (float sampleRate, int sampleSizeInBits, int channels, logical signature, Boolean bigEndian); sampleRate = 44100f; sampleSizeInBits = 16; channels = 2; signed = true; bigEndian = true / false which ever works

Basically the above configuration works on most platforms, including Linux and Windows, haven't tried the Mac at the moment

+2
source

Source: https://habr.com/ru/post/1434931/


All Articles