Trying to get AudioFormat details from Mixer

允我心安 提交于 2020-01-07 03:45:22

问题


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() + "\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:


The Mixer getSourceLines and getTargetLines methods only return open lines - there probably aren't any open when you run your code. Use getSourceLineInfo and getTargetLineInfo instead.

This is the test code I usually use:

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());
   }
}


来源:https://stackoverflow.com/questions/39509597/trying-to-get-audioformat-details-from-mixer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!