Setting the computer volume

I want to create a java program that

  • Reads a line from a file (ex.File text: "Hello")
  • if line = "Hello" then
  • get the volume of the computer if the volume of the computer is> 1, then Volume = volume / 2

I do not know how this part of the volume of this program is. I watched online for the past hour, and I cannot figure out how to do this, or if it is possible.

I forgot to add (I use windows 7)

+1
source share
3 answers

I wrote a utility class to manage the volume, which is used as follows:

Audio.setMasterOutputVolume(0.5f);

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

+5

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

.1 7.0, :

setOutputVolume(3.5f);

. - Windows, - , , ?

+1

, , , , - . ( ). (, -). nircmd.exe - . , :

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

This will set your system volume to 50. To set the volume, you need to select a number from 0 to 65535. If you want to use numbers from 0 to 100, use the method below. It converts the desired volume (using simple mathematical data: 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: Unfortunately, this only works for windows,

"because Java is cross-platform, it cannot do things such as changing the volume or what you want to do to control the OS. To do this, you need to use a unique API level for the operating system." source

+1
source

Source: https://habr.com/ru/post/1784395/


All Articles