【发布时间】:2015-08-07 02:19:48
【问题描述】:
我有两个连接到计算机的麦克风(一个内置麦克风,一个基于 USB)。我想用他们每个人录制相应的音频。
主要记录代码如下:
class Record implements Runnable{
byte bts[] = new byte[10000];
public void run() {
baos = new ByteArrayOutputStream();
try {
stopflag = false;
while(stopflag != true)
{
int cnt = td.read(bts, 0, bts.length);
if(cnt > 0)
{
baos.write(bts, 0, cnt);
}
byte copyBts[] = bts;
bais = new ByteArrayInputStream(copyBts);
ais = new AudioInputStream(bais, af, copyBts.length/af.getFrameSize());
try{
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, af);
sd = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
sd.open(af);
sd.start();
//read from audio stream
int Buffer_Size = 10000;
audioDataBuffer = new byte[Buffer_Size];
intBytes = ais.read(audioDataBuffer, 0,audioDataBuffer.length);
}catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(baos != null){
baos.close();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
td.close();
}
}
}
}
我可以使用this code 列出音频设备。结果是这样的:
OS: Windows 8.1 6.3/x86
Java: 1.8.0_45 (Oracle Corporation)
Mixer: Direct Audio Device: DirectSound Playback [Primary Sound Driver]
Mixer: Direct Audio Device: DirectSound Playback [Speakers / HP (IDT High Definition Audio CODEC)]
Mixer: Direct Audio Device: DirectSound Capture [Primary Sound Capture Driver]
Mixer: Direct Audio Device: DirectSound Capture [Internal Microphone Array (IDT ]**//integrated device**
Mixer: Direct Audio Device: DirectSound Capture [Microphone (2- USB PnP Sound De]**//usb device**
...
现在我如何要求程序一次只从 USB 设备(或集成设备)录制音频?
【问题讨论】: