how do I get Mixer channels layout in java

后端 未结 2 513
我寻月下人不归
我寻月下人不归 2020-12-20 03:17

I thought I can find anything on this great site but now I seem to face the issue with no answer :) Please help!

Thing is, I need to play up to 6 different wav files

2条回答
  •  一个人的身影
    2020-12-20 03:54

    Yes, its an old question but since i was searching to get all the channels, depths and so on i decided to write a small debug method which is usefull if you like to see more details and would like to use java 8 streams :-)

    Enjoy:

    //filter may be all if you want to include microphone 
     public void getDeviceInfos(String filter) {
    
                Stream
                        .of(AudioSystem.getMixerInfo())
                        .forEach(info -> {
                            System.out.println("Mixer Name: " + info.getName());
                            System.out.println("Mixer Description: " + info.getDescription());
                            System.out.println("Mixer Vendor: " + info.getVendor());
    
                            Mixer m = AudioSystem.getMixer(info);
    
                            Stream
                                    .of(Arrays.asList(m.getSourceLineInfo()), filter.equals("record") ? Arrays.asList(m.getTargetLineInfo()) : null)
                                    .flatMap(List::stream)
                                    .collect(Collectors.toList())
                                    .forEach(lineInfo -> {
                                        System.out.println("    info: " + lineInfo);
                                        try {
                                            Line line = AudioSystem.getLine(lineInfo);
                                            if (line instanceof SourceDataLine || line instanceof TargetDataLine) {
    
                                                Arrays.asList(((DataLine.Info) line.getLineInfo()).getFormats()).forEach(format -> {
                                                    System.out.println("Channels: " + format.getChannels());
                                                    System.out.println("Size in Bits: " + format.getSampleSizeInBits());
                                                    System.out.println("Frame Rate: " + format.getFrameRate());
                                                    System.out.println("Frame Size: " + format.getFrameSize());
                                                    System.out.println("Encoding: " + format.getEncoding());
                                                    System.out.println("Sample Rate: " + format.getSampleRate());
    
                                                });
                                            }
                                        } catch (Exception ex) {
                                            ex.printStackTrace();
                                        }
                                    });
                        });
    
        }
    

提交回复
热议问题