Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How can I control system's volume with java?

Squall867May 12 2012 — edited Apr 12 2013
Hi, I'm trying to make a remote audio control and I need a way to set current volume by code.

After looking at java sound APIs I came up with this:
line = mixer.getLine(lineInfo);
boolean opened = line.isOpen() || line instanceof Clip;
if(!opened){
	System.out.println("Line is not open, trying to open it...");
	line.open();
	opened = true;
}
if(line.isControlSupported(FloatControl.Type.VOLUME)){
		FloatControl volumeCtrl = (FloatControl)line.getControl(FloatControl.Type.VOLUME);
               System.out.println("Current volume is: "+volumeCtrl.getValue());
}
I tried this code with every mixer I have (althought I think it's the "SPEAKER" one...) but I always get 1.0 as current volume, no matter what.
So, I'm getting something wrong..can you help me?:(

Comments

gimbal2
Late but just to make it clear, the answer to these kind of questions is the same 999/1000 times. As soon as your question takes this form:

"how do I do SOMETHING in OPERATING SYSTEM using Java",

the answer is:

"You need to write a native library that can do it and call that using JNI or JNA from Java."

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.
898586
You get "1.0" because that's the maximum volume. If you want to change it, you have to set it.
1002736
I'm agree with that opinion
PhHein
You're a year late.
Mod: locking this zombie.
1 - 4
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on May 10 2013
Added on May 12 2012
4 comments
9,644 views