尝试从Mixer获取AudioFormat详细信息

时间:2016-09-15 11:09:55

标签: java javasound

这是我第一次使用Java Sound,我想要实现的是获取源和目标行格式,然后我可以通过创建正确的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");
    }

}

1 个答案:

答案 0 :(得分:2)

Mixer getSourceLinesgetTargetLines方法仅返回打开行 - 运行代码时可能没有任何打开。请改用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());
   }
}