用Java劫持音频?

我一直试图修改一些代码找到
at the bottom of this page,以便用Java劫持系统音频.这是我在captureAudio()中修改的部分:

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

现在,当我运行此代码时,它抛出这个:

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

我已经尝试更改我的格式以适应所需的格式,但异常没有进行,也没有记录任何内容.我究竟做错了什么?

最佳答案 试试以下内容

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

它取自http://docs.oracle.com/javase/tutorial/sound/accessing.html

要创建AudioFormat,请使用

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

以上配置主要适用于大多数平台,包括Linux和Windows,至今尚未尝试过Mac

点赞