用 Java 劫持音频?

2024-02-18

我一直在尝试修改一些发现的代码在本页底部 http://www.java-tips.org/java-se-tips/javax.sound/capturing-audio-with-java-sound-api.html为了用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 http://docs.oracle.com/javase/tutorial/sound/accessing.html

要创建 AudioFormat ,请使用

new AudioFormat(floatsampleRate, intsampleSizeInBits,intchannels,booleansigned,booleanbigEndian); 采样率= 44100f; 样本大小=16; 通道 = 2; 签名=真; bigEndian = true/false 哪个有效

大多数情况下,上述配置适用于大多数平台,包括Linux和Windows,目前还没有尝试过Mac

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

用 Java 劫持音频? 的相关文章

随机推荐