user347284
user347284

Reputation:

Hijack audio with Java?

I have been trying to modify some code found at the bottom of this page in order to hijack system audio with Java. Here's 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 throws this:

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 have tried changing my format to fit with the required format, but the exception doesn't go and nothing is recorded. What am I doing wrong?

Upvotes: 4

Views: 944

Answers (1)

sharadendu sinha
sharadendu sinha

Reputation: 827

Try out 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.
    //... 
}

It 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, boolean signed, boolean bigEndian); sampleRate = 44100f; sampleSizeInBits = 16; channels = 2; signed = true; bigEndian = true/false which ever works

Mostly the above configuration works on most platforms including Linux and windows, have not tried Mac as of now

Upvotes: 2

Related Questions