Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...