6

I want to make a java program that

  1. Reads a line from a file (ex.File text is : "Hello")
  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)

Caleb Guidos
  • 61
  • 1
  • 1
  • 3
  • 1
    Related thread: http://stackoverflow.com/questions/3249550/is-there-a-way-in-java-jna-to-set-the-master-system-volume-reliably-in-xpvi – Igor Feb 11 '11 at 02:23
  • Have at look at this link on [using JavaSound to control the master volume](http://www.coderanch.com/t/492931/java/java/Adjusting-master-volume-Windows-XP). There is some doubt as to how universal it is, though. – Neil Coffey Feb 11 '11 at 02:58

3 Answers3

8

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.

Witek
  • 6,160
  • 7
  • 43
  • 63
  • 1
    Affero license? Or are you giving us permission to copy and relicense here? (As a side note, didn't seem to do anything for me here with Win 7, has anybody gotten it to work?) – rogerdpack Sep 04 '13 at 19:29
  • 2
    Yes, I give you permission to do whatever you like with https://github.com/Kunagi/ilarkesto/blob/master/src/main/java/ilarkesto/media/Audio.java – Witek Sep 05 '13 at 13:13
  • When I use this class, I get “Master output port not found”. I’m using a mac, and my audio output is set to internal speakers (I’m on a MacBook Pro late 2013 model). – Michael Sims Feb 06 '15 at 07:09
  • I collected all of the Line output from the getMasterOutputLine method, and pasted it into a new answer since I cannot place any reasonably formatted text into a comment, not to mention there are not enough characters available in a comment to paste all of the information. My thoughts are that perhaps you can use this output to help me modify the test so that it returns the correct Line. – Michael Sims Feb 06 '15 at 07:28
  • 3
    Did nothing on my Windows 10... anyone gotten this to work? – user1111929 Jul 26 '16 at 15:42
  • on windows 8 method getMasterOutputVolume() return null – Elron Apr 10 '17 at 12:44
  • 1
    and Audio.setMasterOutputVolume(0.5f); throws "RuntimeException: Master output port not found" – Elron Apr 10 '17 at 12:49
5

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

Just_Paul
  • 67
  • 1
  • 9
3

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?

Michael Sims
  • 2,360
  • 1
  • 16
  • 29