Getting the system audio levels in Java

前提是你 提交于 2019-12-19 09:18:10

问题


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 something similar) as a percentage of the maximum setting. I might also want to display the current sound level as a percentage of highest possible sound level, but this is not needed.


回答1:


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.




回答2:


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



回答3:


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).




回答4:


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



来源:https://stackoverflow.com/questions/4211439/getting-the-system-audio-levels-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!