This is my first try with Java Sound and what I'm trying to achieve is getting the source and target lines format so I can then listen to the data and record it to a file by creating the correct AudioFormat object with the details obtained, but when trying to print all the details through the Java console, nothing is printed out. As I said this is my very first steps with Java Sound and I don't really know if there's a simpler approach.
This is the test code I have so far...
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.DataLine.Info;
import javax.sound.sampled.Line;
import javax.sound.sampled.Mixer;
public class SoundTest {
public static void main(String[] args) {
for (javax.sound.sampled.Mixer.Info info : AudioSystem.getMixerInfo()) {
System.out.println(info.getName() + " " +
info.getDescription() + " " +
info.getVendor() + "
");
}
Mixer mixer = (Mixer) AudioSystem.getMixer(AudioSystem.getMixerInfo()[4]);
System.out.println("Mixer: " + mixer.getMixerInfo().getName());
Line[] target = mixer.getTargetLines();
Line[] source = mixer.getSourceLines();
DataLine.Info objTarget, objSource;
for (Line t : target) {
objTarget = (Info) t.getLineInfo();
for (AudioFormat format : objTarget.getFormats()) {
formatInfo(format); // prints nothing.
}
}
for (Line t : source) {
objSource = (Info) t.getLineInfo();
for (AudioFormat format : objSource.getFormats()) {
formatInfo(format); // prints nothing.
}
}
}
private static void formatInfo(AudioFormat format) {
System.out.println("Number of channels: " + format.getChannels() + "
" + "Sample rate: "
+ format.getSampleRate() + "
" + "Frame rate: " + format.getFrameRate() + "
" + "Frame size: "
+ format.getFrameSize() + "
" + "Sample size bits: " + format.getSampleSizeInBits() + "
"
+ "Encoding: " + format.getEncoding() + "
");
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…