我需要从线路输入端口而不是麦克风捕获声音。
虽然我完成了从麦克风录音,但我无法完成从线路输入端口或特定端口捕获声音。我该如何处理这个问题?
import javax.sound.sampled.*;
/**
*
* @author d07114915
*
* Class to get a mixer with a specified recordable audio format from a specified port
* For instance get a 44.1kHz 16bit record line for a "line in" input
*/
public class MixerMatcher {
private static final String THE_INPUT_TYPE_I_WANT = "MICROPHONE";
private static final String THE_NAME_OF_THE_MIXER_I_WANT_TO_GET_THE_INPUT_FROM = "Realtek HD Audio Input";
private static final AudioFormat af = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
44100.0F,
16,
2,
2 * 2,
44100.0F,
false);
private static final DataLine.Info targetDataLineInfo = new DataLine.Info(TargetDataLine.class, af);
private static final Port.Info myInputType = new Port.Info((Port.class), THE_INPUT_TYPE_I_WANT, true);
private static TargetDataLine targetDataLine = null;
public static void main(String[] args) {
Mixer portMixer = null;
Mixer targetMixer = null;
try {
for (Mixer.Info mi : AudioSystem.getMixerInfo()) {
// System.out.println("-" +mi.getName() + "-");
if (mi.getName().equals(THE_NAME_OF_THE_MIXER_I_WANT_TO_GET_THE_INPUT_FROM)) {
System.out.println("Trying to get portMixer for :" + mi.getName());
portMixer = getPortMixerInfoFor(mi);
if (portMixer != null) {
System.out.println(portMixer.getMixerInfo().toString());
targetMixer = AudioSystem.getMixer(mi);
break;
}
}
}
if (targetMixer != null) {
targetMixer.open();
targetDataLine = (TargetDataLine) targetMixer.getLine(targetDataLineInfo);
System.out.println("Got TargetDataLine from :" + targetMixer.getMixerInfo().getName());
portMixer.open();
Port port = (Port) portMixer.getLine(myInputType);
port.open();
Control[] controls = port.getControls();
System.out.println((controls.length > 0 ? "Controls for the "+ THE_INPUT_TYPE_I_WANT + " port:" : "The port has no controls."));
for (Control c : controls) {
System.out.println(c.toString());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
//return the portMixer that corresponds to TargetMixer
private static Mixer getPortMixerInfoFor(Mixer.Info mixerInfo) {
//Check this out for interest
//http://www.java-forum.org/spiele-multimedia-programmierung/94699-java-sound-api-zuordnung-port-mixer-input-mixer.html
try {
// get the requested mixer
Mixer targetMixer = AudioSystem.getMixer(mixerInfo);
targetMixer.open();
//Check if it supports the desired format
if (targetMixer.isLineSupported(targetDataLineInfo)) {
System.out.println(mixerInfo.getName() + " supports recording @ " + af);
//now go back and start again trying to match a mixer to a port
//the only way I figured how is by matching name, because
//the port mixer name is the same as the actual mixer with "Port " in front of it
// there MUST be a better way
for (Mixer.Info portMixerInfo : AudioSystem.getMixerInfo()) {
String port_string = "Port ";
if ((port_string + mixerInfo.getName()).equals(portMixerInfo.getName())) {
System.out.println("Matched Port to Mixer:" + mixerInfo.getName());
Mixer portMixer = AudioSystem.getMixer(portMixerInfo);
portMixer.open();
//now check the mixer has the right input type eg LINE_IN
boolean lineTypeSupported = portMixer.isLineSupported((Line.Info) myInputType);
System.out.println(portMixerInfo.getName() +" does " + (lineTypeSupported? "" : "NOT") + " support " + myInputType.getName());
if (lineTypeSupported) {
portMixer.close();
targetMixer.close();
return portMixer;
}
portMixer.close();
}
}
}
targetMixer.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
运行这个我得到:
Trying to get portMixer for :Realtek HD Audio Input
Realtek HD Audio Input supports recording @ PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian
Matched Port to Mixer:Realtek HD Audio Input
Port Realtek HD Audio Input does support MICROPHONE
Port Realtek HD Audio Input, version 5.10
Got TargetDataLine from :Realtek HD Audio Input
Controls for the MICROPHONE port:
Mic Volume Control containing Select, Microphone Boost, Volume, and Balance Controls.
将混音器和输入类型首选项分别更改为“USB 声音设备”和“LINE_IN”,我得到以下信息:(注意“设备”一词后的混音器名称中有 8 个空格,但它们不会显示在此网络上页!)
Trying to get portMixer for :USB Sound Device
Trying to get portMixer for :USB Sound Device
USB Sound Device supports recording @ PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian
Matched Port to Mixer:USB Sound Device
Port USB Sound Device does support LINE_IN
Port USB Sound Device , version 0.16
Got TargetDataLine from :USB Sound Device
Controls for the LINE_IN port:
Line Control containing Select, Mute, Volume, and Balance Controls.
此处 USB 声卡显示一个输入端口和一个输出端口,因此其中一个不支持 LINE_IN,这可能是因为它是一个输出,因此可能允许录制“立体声混音”或其他一些输出类型
希望这能工作并对某人有所帮助....因为Java文档相当模糊...在Windows上测试过,但我认为Linux无法识别诸如LINE_IN之类的端口名称,因此您需要检查操作系统端口是,可能还有其他一些事情,比如需要混音器名称的子字符串等......在我的 Linux 上,麦克风被称为“Capture”......
查看jsresources.org 常见问题以获取更多信息
错误等的任何改进让我知道。
d07114915
我也没有想到这一点,但你实际上需要
mixer.getSourceLineInfo()
因为这是来自混音器的视图......真的很混乱!
targetDataLine 是一条可记录的输入线,但要查看这些线可能来自哪里,您需要查找混音器的端口,即(MICROPHONE、LINE_IN、SPDIF、),因此您需要调用
混合器.getSourceLineInfo()
这只给出了端口。例如,这些可用于控制 LINE_IN 输入的录音音量,但您不能直接从 PORT 对象进行录音。
您需要使用 DataLine.Info targetDataLineInfo = new DataLine.Info(TargetDataLine.class, AudioFormat);
其中 AudioFormat 是一些用户定义的格式,例如
audioFormat = new AudioFormat(
Encoding.PCM_SIGNED,
sampleRate,
bitRate,
monoOrStereo,
monoOrStereo * 2, //
sampleRate,
false);
然后你从首选的 Mixer 中得到这条线:
Mixer.getLine(audioFormat);
这将为您提供一条可记录的线路,正如您可能已经完成的那样......
我想不通的是,和你一样,如何选择 LINE_IN 之类的 Port 并创建 Port Object 可以控制的匹配 TargetDataLine,我已经搜索和搜索了..
()
任何有工作示例的人都在那里..
尝试这个。如果它有效,请告诉我,因为我无法在我的笔记本电脑上完全自己测试它。然后,您可以编写自己的函数来获取具有指定输入类型和/或所需混音器的PortMixer
、ActualMixer
和。TargateDataLine
private void testGettingInput() {
//Check this out for interest
//http://www.java-forum.org/spiele-multimedia-programmierung/94699-java-sound-api-zuordnung-port-mixer-input-mixer.html
final String newLine = System.getProperty("line.separator");
final String inputTypeString = "LINE_IN"; // or COMPACT_DISC or MICROPHONE etc ...
final Port.Info myInputType = new Port.Info((Port.class), inputTypeString, true);
final AudioFormat af = new AudioFormat(
Encoding.PCM_SIGNED,
44100.0F,
16,
2,
2 * 2,
44100.0F,
false);
final DataLine.Info targetDataLineInfo = new DataLine.Info(TargetDataLine.class, af);
TargetDataLine targetDataLine;
//Go through the System audio mixers
for (Mixer.Info mixerInfo : AudioSystem.getMixerInfo()) {
try {
Mixer targetMixer = AudioSystem.getMixer(mixerInfo);
targetMixer.open();
//Check if it supports the desired format
if (targetMixer.isLineSupported(targetDataLineInfo)) {
System.out.println(mixerInfo.getName() + " supports recording @" + af);
//now go back and start again trying to match a mixer to a port
//the only way I figured how is by matching name, because
//the port mixer name is the same as the actual mixer with "Port " in front of it
// there MUST be a better way
for (Mixer.Info mifo : AudioSystem.getMixerInfo()) {
String port_string = "Port ";
if ((port_string + mixerInfo.getName()).equals(mifo.getName())) {
System.out.println("Matched Port to Mixer:" + mixerInfo.getName());
Mixer portMixer = AudioSystem.getMixer(mifo);
portMixer.open();
portMixer.isLineSupported((Line.Info) myInputType);
//now check the mixer has the right input type eg LINE_IN
if (portMixer.isLineSupported((Line.Info) myInputType)) {
//OK we have a supported Port Type for the Mixer
//This has all matched (hopefully)
//now just get the record line
//There should be at least 1 line, usually 32 and possible unlimited
// which would be "AudioSystem.Unspecified" if we ask the mixer
//but I haven't checked any of this
targetDataLine = (TargetDataLine) targetMixer.getLine(targetDataLineInfo);
System.out.println("Got TargetDataLine from :" + targetMixer.getMixerInfo().getName());
return;
}
}
}
System.out.println(newLine);
}
targetMixer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
熟悉TargetDataLine
班级。此外,来自它自己的 Javadoc:
通过使用适当的 DataLine.Info 对象调用 Mixer 的 getLine 方法,可以从混音器中获取目标数据行。
具体来说,我还将查看Mixer.getTargetLineInfo()
并检查返回的输出以选择与您正在寻找的线路输入端口匹配的线路。