1

I wrote a Java program to change the volume of an audio clip (a .wav) with a command line argument, but that only seems to be somewhat of a "soft" control in that it doesnt actually change the master volume of the actual machine its running on and I have to manually press the increase or decrease volume buttons on my latop to change it further. Is Java capable of changing the actual computer's volume? If so, how?

I should add, Im on Windows 10.

jquigs62
  • 19
  • 5
  • Does this answer your question? [Set Computer Volume](https://stackoverflow.com/questions/4964947/set-computer-volume) – dan1st Feb 18 '21 at 19:38
  • Unfortunately not, Ive read through all those threads already. They all kinda self-reference eachother and most of the solutions posted in them either dont work or are only regarding controlling the sound of the audio file but not the system sound itself (think changing the volume slider in a youtube video without actually changing your computers volume). What I want is to actually change my computers volume via a java application – jquigs62 Feb 18 '21 at 19:45
  • What about the first two answers? What does not work, there? – dan1st Feb 18 '21 at 19:50
  • The second one I cant use at all because it requires an external tool in order to help adjust the computer volume. I can only do this in the confines of a java program. The first one seems to not work with windows 10 and also seems to only be a "soft control" as i referred to before – jquigs62 Feb 18 '21 at 19:55
  • So what about the first answer? – dan1st Feb 18 '21 at 19:56
  • You could use the Robot class to simulate pressing the f7 & f8 keys, but it would be a rough fix. – Nicolas125841 Feb 18 '21 at 19:59
  • Part of the problem is that Java was designed to be platform independent, so certain things that are platform dependent are difficult to do. – NomadMaker Feb 19 '21 at 16:42
  • the "The first one seems to not work with windows 10 " is correct - although it has an effect - I have yet to figure out why it isn't materialzed in the general volume. As for the second I dont see what is "hard" for you since you reject all solutions. I would suggest a script with the robot as indicated above: you can define the steps that you would use manually and do that with the robot - I have done it for some other task - unless you want to deal with MS programming which is what JNA does - - although the computer has to be left to itself for that time period. Any way good luck – gpasch Feb 20 '21 at 03:43
  • I think it would be worthwhile adding your intended purpose for this. There is the potential that a more elegant solution exists that results in the same effect. – fdcpp Feb 20 '21 at 09:22

3 Answers3

0

A crufty, platform-dependent solution:

  1. Download NirCmd.
  2. Ship the exe which as far as I know is 'dependency free' (no installation needed), inside the jar.
  3. To change the volume, unpack this exe from your jar to a temp dir (this is a tad security-wise tricky).
  4. Run it, using ProcessBuilder. It can change the system volume.

It's not great, but, it should work.

NB: Please check the licensing conditions of NirCmd; this may not be quite acceptable, you may have to show that license as part of your app. Dont take legal advice from a stack overflow answer.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
0

I was ready to let this question be until I came across the word impossible. Before giving up, maybe take a look at the JNA library. This library is built specifically for accessing native code.

JNA provides Java programs easy access to native shared libraries without writing anything but Java code - no JNI or native code is required. This functionality is comparable to Windows' Platform/Invoke and Python's ctypes.

JNA allows you to call directly into native functions using natural Java method invocation.

There is an active forum listed in the git README. If nothing else, you can ask/explore whether this issue is one that fits within the capabilities of the library.

But I have to admit, even if possible, it seems like it would require considerable effort as well as being dubious in concept: the setting of the computer hardware's volume is properly in the hands of the computer operator.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41
0

Hopefully Windows 11 will have a feature that allows you to change the master volume. What about trying to get the program to trigger the existing software in Windows 10 that changes the volume when you press the keys?

JavaScript --> Binary Code In Windows 10 --> Master Volume Bypass

Adrian
  • 1