[英]How to record audio with the specified audio device in Java?
I have two microphones attached to the computer (one internal and one USB based). 我有两个连接到计算机的麦克风(一个内置麦克风,一个基于USB)。 I want to record corresponding audio with each of them. 我想与他们每个录制对应的音频。
The main record codes are something like: 主要记录代码如下:
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();
}
}
}
}
And I can list audio devices with this code . 我可以用此代码列出音频设备。 The result is like: 结果是这样的:
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**
...
Now how can I ask the program to just record audio from the usb device (or the integrated device) at a time? 现在,我如何才能要求该程序一次只记录USB设备(或集成设备)的音频?
Find the Mixer
that you want to use and replace your 找到您要使用的Mixer
并更换
sd = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
line with 符合
sd = (SourceDataLine) mixer.getLine(dataLineInfo);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.