Java Sound API - 捕获麦克风
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3705581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Sound API - capturing microphone
提问by myusuf3
I have been reading up on the Sound API for Java for a couple of days I am unable to make sense of it. I am decent programmer, I just having difficulty getting my head around the API.
几天来我一直在阅读 Java 的声音 API,但我无法理解它。我是一个不错的程序员,我只是很难理解 API。
I have been trying to capture audio from my microphone and display a wave graph in real time.
我一直在尝试从我的麦克风捕获音频并实时显示波形图。
I am having trouble capturing audio, they say in the tutorial to do this, but I cant seem to get it to work.
我在捕获音频时遇到问题,他们在教程中说要这样做,但我似乎无法让它工作。
Any suggestions and help would be much appreciated, a line by line answer would be ideal.
任何建议和帮助将不胜感激,逐行回答将是理想的。
Please and thank you.
谢谢,麻烦您了。
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;
public class FindLine {
public static void main (String[] args){
AudioFormat format = new AudioFormat(22000,16,2,true,true);
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 ...
}
}
}
采纳答案by DannyM
This will get you the default one set by your OS.
这将为您提供操作系统设置的默认值。
AudioFormat format = new AudioFormat(8000.0f, 16, 1, true, true);
TargetDataLine microphone = AudioSystem.getTargetDataLine(format);
To select a particular input device (TargetDataLine) it is better to enumerate the mixers and filter the name of the Mixer you want.
要选择特定的输入设备 (TargetDataLine),最好枚举混音器并过滤所需的混音器名称。
Mixer.Info[] mixerInfos = AudioSystem.getMixerInfo();
for (Mixer.Info info: mixerInfos){
Mixer m = AudioSystem.getMixer(info);
Line.Info[] lineInfos = m.getSourceLineInfo();
for (Line.Info lineInfo:lineInfos){
System.out.println (info.getName()+"---"+lineInfo);
Line line = m.getLine(lineInfo);
System.out.println("\t-----"+line);
}
lineInfos = m.getTargetLineInfo();
for (Line.Info lineInfo:lineInfos){
System.out.println (m+"---"+lineInfo);
Line line = m.getLine(lineInfo);
System.out.println("\t-----"+line);
}
}