我正在尝试让 Java 在两个输出(前后音频插孔)中播放不同的立体声音频。
我的声卡配置为独立处理两个输出,在 Windows 混音器中我可以让它们分别发出测试声音,所以这不是卡问题。
我尝试了Change Mixer上的方法,通过使用不同的混音器获取两个剪辑,使用不同的输出在 java 中输出声音AudioSystem.getClip(AudioSystem.getMixerInfo()[i]);
。但是,这行代码仅适用于 Java Sound Audio Engine ( AudioSystem.getMixerInfo()[0]
),它以 Windows 的默认音频输出方式输出。其他任何东西都会抛出
java.lang.IllegalArgumentException: Line unsupported: interface Clip supporting format PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian
以下示例代码生成 5 秒长的白噪声并播放 1 秒然后结束。它打印混音器信息,如我如何在 java 中获取混音器通道布局所见。它当前输出到“Java Sound Audio Engine”混音器,尝试更改为任何其他混音器会引发上述异常。
import java.io.ByteArrayInputStream;
import java.security.SecureRandom;
import java.util.Random;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.Line;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
public class Main {
static int SAMPLE_RATE = 44100;
static AudioFormat format = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED, // Encoding
SAMPLE_RATE, // sample rate
8, // sample size in bits
2, // channels
4, // frame size
SAMPLE_RATE, // frame rate
true); // is big endian
static int DURATION = 5;
Thread soundThread;
// Noise audio
static AudioInputStream inputStream = new AudioInputStream(new ByteArrayInputStream(generateNoise(DURATION*2*SAMPLE_RATE)), format, DURATION*SAMPLE_RATE);
public static void main(String[] args) {
try {
// https://stackoverflow.com/questions/12863081/how-do-i-get-mixer-channels-layout-in-java
Mixer.Info[] mi = AudioSystem.getMixerInfo();
for (Mixer.Info info : mi) {
System.out.println("info: " + info);
Mixer m = AudioSystem.getMixer(info);
System.out.println("mixer " + m);
Line.Info[] sl = m.getSourceLineInfo();
for (Line.Info info2 : sl) {
System.out.println(" info: " + info2);
Line line = AudioSystem.getLine(info2);
if (line instanceof SourceDataLine) {
SourceDataLine source = (SourceDataLine) line;
DataLine.Info i = (DataLine.Info) source.getLineInfo();
for (AudioFormat format : i.getFormats()) {
System.out.println(" format: " + format);
}
}
}
System.out.println("");
}
// Code only works for AudioSystem.getMixerInfo()[0]
final Clip clip = AudioSystem.getClip(AudioSystem.getMixerInfo()[0]);
clip.open(inputStream);
Thread soundThread = new Thread(new Runnable() {
@Override
public void run() {
try {
clip.loop(Clip.LOOP_CONTINUOUSLY);
Thread.sleep(1000);
clip.close();
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
}
}
});
soundThread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
private static byte[] generateNoise(int size) {
byte[] out = new byte[size];
Random r = new SecureRandom();
r.nextBytes(out);
return out;
}
}
这是混音器信息。此代码在连接两个输出插孔的情况下运行,Windows 将其识别为两个不同的输出设备(扬声器和耳机)。看起来只有 Java 的音频引擎可以播放声音。
info: Java Sound Audio Engine, version 1.0
mixer com.sun.media.sound.HeadspaceMixer@22c84d9
info: interface SourceDataLine supporting 8 audio formats
format: PCM_SIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame,
format: PCM_UNSIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame,
format: PCM_SIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame,
format: PCM_UNSIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame,
format: PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, big-endian
format: PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, little-endian
format: PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian
format: PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, little-endian
info: interface Clip supporting 8 audio formats, and buffers of 0 to 4194304 bytes
info: Microsoft ?T?E???h, version Unknown Version
mixer com.sun.media.sound.SimpleInputDevice@7e0df503
info: Stereo Mixer (Realtek High Defi, version Unknown Version
mixer com.sun.media.sound.SimpleInputDevice@4650d89c
info: Port Realtek HD Audio 2nd output (Re, version 6.1
mixer com.sun.media.sound.PortMixer@65bd0dd4
info: Port Stereo Mixer (Realtek High Defi, version 6.1
mixer com.sun.media.sound.PortMixer@78b5f53a
info: ?}?X? source port
info: Port Speakers (Realtek High Definiti, version 6.1
mixer com.sun.media.sound.PortMixer@b37c60d
由于其他限制,我正在使用 Java 6。此外,一些名称可能因为我在日语环境中而被破坏,并且 Eclipse 不会以正确的编码获取名称(我尝试将所有内容更改为 UTF-8 和 Shift_JIS,但没有任何改变,但我猜它不相关到这个问题)。
换句话说,它看起来无法在来自不同混音器的剪辑中输出,因为 Java 只能输出到 Windows 默认音频设备的“Java 声音音频引擎”。有什么方法可以让其他混音器工作吗?是否有使用多个音频输出的替代方法?
更新:似乎这个问题已经解决了,但我仍然无法让它工作。我禁用了“立体声混音”录音设备并运行了该链接中给出的示例代码,并得到了以下输出:
MIXER 0: Java Sound Audio Engine, version 1.0
OUTPUT LINE (SourceDataLine) 0: interface SourceDataLine supporting 8 audio formats
OUTPUT LINE (SourceDataLine) 1: interface Clip supporting 8 audio formats, and buffers of 0 to 4194304 bytes
MIXER 1: Port Realtek HD Audio 2nd output (Re, version 6.1
INPUT LINE (TargetDataLine) 0: HEADPHONE target port
MIXER 2: Port Speakers (Realtek High Definiti, version 6.1
INPUT LINE (TargetDataLine) 0: SPEAKER target port
所以看起来只有 Java Sound Audio Engine 可以输出声音,因为前后音频插孔的混音器都被视为输入线。通过 Windows 默认播放设备中的 Clip 或 SourceDataLine 输出播放音频。
此外,看起来他们通过使用 DirectAudio解决了问题,但我仍然不知道如何使用它们。