Hijack audio with Java?

橙三吉。 提交于 2021-02-08 16:41:36

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/12483747/hijack-audio-with-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!