-1

我正在尝试从通过 USB 连接到我的电脑的外部混音器获取线路。所以我写了一个简单的程序来列出所有的混音器以及它们各自的源行(输出)和目标行(输入),它可以正常工作:

import javax.sound.sampled.*;

public class TestResources {
    public static void main(String args[]) {
        try {
            Mixer.Info [] mixers = AudioSystem.getMixerInfo();
            for(int i = 0 ; i< mixers.length; i ++) {
                System.out.println((i+1)+". " + mixers[i].getName() + " --> " + mixers[i].getDescription() );

                Line.Info [] sourceLines = AudioSystem.getMixer(mixers[i]).getSourceLineInfo();
                System.out.println("\tSource Lines:" );
                for(int j = 0; j< sourceLines.length; j++) {
                    System.out.println("\t" + (j+1) + ". " + sourceLines[j].toString() );
                }
                System.out.println();

                Line.Info [] targetLines = AudioSystem.getMixer(mixers[i]).getTargetLineInfo();                 
                System.out.println("\tTarget Lines:" );
                for(int j = 0; j< targetLines.length; j++) {
                    System.out.println("\t" + (j+1) + ". " + targetLines[j].toString() );

                }       
                System.out.println("\n" );
            }           
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}

但是,当我真正尝试获得我需要的线路时,如下所示:

    AudioFormat format = new AudioFormat(44100, 16, 2, true, true);
    Mixer.Info [] mixers = AudioSystem.getMixerInfo();
    final TargetDataLine microphone = AudioSystem.getTargetDataLine(format, mixers[2]);

我收到一条错误消息,指出该行不支持该格式:

java.lang.IllegalArgumentException: Line unsupported: interface TargetDataLine supporting format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 8 bytes/frame, big-endian

如何扩展初始程序以打印每行支持的格式?提前致谢。

4

1 回答 1

2

此方法打印对象支持的格式Line.Info

static void showLineInfoFormats(final Line.Info lineInfo)
{
  if (lineInfo instanceof DataLine.Info)
   {
     final DataLine.Info dataLineInfo = (DataLine.Info)lineInfo;

     Arrays.stream(dataLineInfo.getFormats())
           .forEach(format -> System.out.println("    " + format.toString()));
   }
}
于 2018-12-16T16:18:20.440 回答