Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
534 views
in Technique[技术] by (71.8m points)

java - Trying to get AudioFormat details from Mixer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...