Getting the system audio levels in Java

前端 未结 4 1154
故里飘歌
故里飘歌 2021-01-13 19:15

How does one get the master volume in Java? I want to make a program that displays (NOT CHANGE) this value (probably on a JProgressBar or somet

相关标签:
4条回答
  • 2021-01-13 19:36

    You could be a little less abrasive rather than say "Does not help me".

    Otherwise, here is the big picture, using the Sound API mentioned by AlexR's example: (there are a lot of practical details on that forum thread, and there is some Oracle documentation for Processing Audio with Controls)

    • Once you get a Line object that represents the sound output line that you wish to control, it is rather trivial to get the volume control:

      line.open(); // May be necessary if the line is not already opened.
      FloatControl volumeControl = (FloatControl) masterLine.getControl(FloatControl.Type.VOLUME);
      
    • Getting the "Master Line" object is, at best, platform-dependant. You will have to list the Mixers and the Lines at runtime using the AudioSystem static methods in order to determine which line you want. It can also happen that the master line will not support the volume control directly, but only through a CompoundControl. (through a hierarchy that is, you guessed it, platform-dependant).

    0 讨论(0)
  • 2021-01-13 19:40

    I am not entirely sure, but you could have a look at the (Java Media Framework - JMF). You are able to control the sound via that library, so I would assume you can get the details about it too. It might just be the application's sound level, so I might be wrong.

    0 讨论(0)
  • 2021-01-13 19:48

    Nico is right. Here is an article and example that might help you.

    http://onjava.com/pub/a/onjava/excerpt/jenut3_ch17/index.html
    http://onjava.com/onjava/excerpt/jenut3_ch17/examples/SoundPlayer.java

    0 讨论(0)
  • 2021-01-13 19:55

    Although JProgressBar is not used for this
    This might Help

    package test;
    
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.FloatControl;
    import javax.sound.sampled.Line;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.Mixer;
    import javax.swing.BoxLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JProgressBar;
    
    public class SoundMeter {
    
    JFrame j;
    
    public SoundMeter() {
        j = new JFrame("SoundMeter");
        j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        j.setLayout(new BoxLayout(j.getContentPane(), BoxLayout.Y_AXIS));
        printMixersDetails();
        j.setVisible(true);
    }
    public void printMixersDetails(){
        javax.sound.sampled.Mixer.Info[] mixers = AudioSystem.getMixerInfo();
        System.out.println("There are " + mixers.length + " mixer info objects");  
        for(int i=0;i<mixers.length;i++){
            Mixer.Info mixerInfo = mixers[i];
            System.out.println("Mixer Name:"+mixerInfo.getName());
            Mixer mixer = AudioSystem.getMixer(mixerInfo);
            Line.Info[] lineinfos = mixer.getTargetLineInfo();
            for(Line.Info lineinfo : lineinfos){
                System.out.println("line:" + lineinfo);
                try {
                    Line line = mixer.getLine(lineinfo);
                    line.open();
                    if(line.isControlSupported(FloatControl.Type.VOLUME)){
                        FloatControl control = (FloatControl) line.getControl(FloatControl.Type.VOLUME);
                        System.out.println("Volume:"+control.getValue());   
                        JProgressBar pb = new JProgressBar();
                        // if you want to set the value for the volume 0.5 will be 50%
                        // 0.0 being 0%
                        // 1.0 being 100%
                        //control.setValue((float) 0.5);
                        int value = (int) (control.getValue()*100);
                        pb.setValue(value);
                        j.add(new JLabel(lineinfo.toString()));
                        j.add(pb);
                        j.pack();
                    }
                } catch (LineUnavailableException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main(String[] args) {
        new SoundMeter();
    }
    }
    
    0 讨论(0)
提交回复
热议问题