开发者

Set Computer Volume

I want to make a java program that

  1. Reads a line from a file (ex.File text is : "Hel开发者_如何学运维lo")
  2. if the Line = "Hello" then
  3. get computer volume if computer volume is > 1 then Volume=volume / 2

I don't know how do the volume part, of this program. I have looked online for the past hour and I can't seem to get an idea on how to do this, or if it is even possible.

I forgot to add (I'm using windows 7)


I have written an utility class for controlling volume which is used like that:

Audio.setMasterOutputVolume(0.5f);

Source code is here: https://github.com/Kunagi/ilarkesto/blob/master/src/main/java/ilarkesto/media/Audio.java

You can copy and use the code.


I know the question is old, but maybe my answer helps anyone else. I've found an useful tool (download-link is at the bottom of the page). With this tool you can adjust your system volume and do many other stuff (for example open your cd-drive). Just download it and put nircmd.exe somewhere on your hard drive. Then write the following to adjust your volume:

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(pathToNircmdexe + " setsysvolume 32767.5");

This will set your system volume to 50. To set your volume you will need choose a number between 0 and 65535. If you want to use numbers from 0 to 100 use the method below. It will convert your desired volume (By using simple maths :D)

public void setSystemVolume(int volume)
{
    if(volume < 0 || volume > 100)
    {
        throw new RuntimeException("Error: " + volume + " is not a valid number. Choose a number between 0 and 100");
    }

    else
    {
        double endVolume = 655.35 * volume;

        Runtime rt = Runtime.getRuntime();
        Process pr;
        try 
        {
            pr = rt.exec(nircmdFilePath + " setsysvolume " + endVolume);
            pr = rt.exec(nircmdFilePath + " mutesysvolume 0");

        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
}

Note: Sadly this only works for windows,

"because Java is cross-platform, it cannot do platform-specific stuff like changing the volume or whatever you want to do to control the OS. You need to use the operating system's unique API layer to do it." source


Here is how I successfully set the systems master volume using a terminal command (I am using a Mac running OS X 10.10):

public static void setOutputVolume(float value)
{
    String command = "set volume " + value;
    try
    {
        ProcessBuilder pb = new ProcessBuilder("osascript","-e",command);
        pb.directory(new File("/usr/bin"));
        StringBuffer output = new StringBuffer();
        Process p = pb.start();
        p.waitFor();
        BufferedReader reader =
                new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while ((line = reader.readLine())!= null)
        {
            output.append(line + "\n");
        }
        System.out.println(output);
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
}

value can be anywhere from .1 to 7.0 and you would call the method like this:

setOutputVolume(3.5f);

This would put the system volume at roughly half of it’s max. I do not know of any command line options for setting system volume in Windows, if anyone knows of any, perhaps they can chime in?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜