试图从 Mixer 获取 AudioFormat 详细信息

Trying to get AudioFormat details from Mixer

这是我第一次尝试 Java 声音,我想要实现的是获取源和目标行格式,这样我就可以收听数据并通过创建使用获得的详细信息正确的 AudioFormat 对象,但是当尝试通过 Java 控制台打印所有详细信息时,没有打印出任何内容。正如我所说,这是我使用 Java Sound 的第一步,我真的不知道是否有更简单的方法。

这是我目前的测试代码...

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() + "\n");
        }

        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() + "\n" + "Sample rate: "
                + format.getSampleRate() + "\n" + "Frame rate: " + format.getFrameRate() + "\n" + "Frame size: "
                + format.getFrameSize() + "\n" + "Sample size bits: " + format.getSampleSizeInBits() + "\n"
                + "Encoding: " + format.getEncoding() + "\n");
    }

}

Mixer getSourceLinesgetTargetLines 方法 return open 行 - 当你 运行 你的代码。请改用 getSourceLineInfogetTargetLineInfo

这是我常用的测试代码:

public static void displayMixerInfo()
{
  Mixer.Info [] mixersInfo = AudioSystem.getMixerInfo();

  for (Mixer.Info mixerInfo : mixersInfo)
   {
     System.out.println("Mixer: " + mixerInfo.getName());

     Mixer mixer = AudioSystem.getMixer(mixerInfo);

     Line.Info [] sourceLineInfo = mixer.getSourceLineInfo();
     for (Line.Info info : sourceLineInfo)
       showLineInfo(info);

     Line.Info [] targetLineInfo = mixer.getTargetLineInfo();
     for (Line.Info info : targetLineInfo)
       showLineInfo(info);
   }
}


private static void showLineInfo(final Line.Info lineInfo)
{
  System.out.println("  " + lineInfo.toString());

  if (lineInfo instanceof DataLine.Info)
   {
     DataLine.Info dataLineInfo = (DataLine.Info)lineInfo;

     AudioFormat [] formats = dataLineInfo.getFormats();
     for (final AudioFormat format : formats)
       System.out.println("    " + format.toString());
   }
}