简体   繁体   中英

Is there any way to change the system volume with a Java Program?

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM