如何通过Java程序调节扬声器音量?

发布于 2024-08-10 19:55:08 字数 184 浏览 1 评论 0原文

我运行的是 Win Vista,在窗口右下角的时钟旁边有一个扬声器图标,我可以单击它并调整音量,我想知道我的 Java 程序中是否有办法自动执行此操作?

例如,当我的Java程序启动时,它将音量调至80,当程序退出时,它将音量调回原来的水平。如果有办法达到这种效果,我不介意使用 Runtime.getRuntime().exec() 。

I'm running Win Vista, at the lower right side of the window there is a speaker icon next to the clock, I can click on it and adjust the volume, I wonder if there is a way in my Java program to do this automatically?

For instance, when my Java program starts, it turns the volume to 80, and when the program exits, it changes the volume back to the original level. I don't mind using Runtime.getRuntime().exec() if there is a way to achieve this effect.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

耳钉梦 2024-08-17 19:55:09

Java 的主要前提之一是用它编写的应用程序应该在任何平台上运行。这就是为什么他们在 Java 1.4 SDK 中放弃了对环境变量的支持,但后来又重新启用了它。这就是为什么无法使用“cls”之类的命令来清理 Java 控制台,因为它可以在某些平台上工作,但不能在其他平台上工作。

话虽如此,您将无法通过 Java 做到这一点。您可以使用 C++ 创建 JNI DLL,或者使用 C++ 或 C# 创建应用程序来执行此操作。

有关在 C++ 中执行此操作的更多信息:

更改卷 win32 c++

One of the main premises of Java is that an application written on it should work in any platform. That's why they dropped the support for environment variables in the Java 1.4 SDK but later re-enabled it. That's why there's no way to clean the Java console with a command like "cls", as it could work in some platforms but not in others.

That being said, you won't be able to do from Java. You can either create a JNI DLL in C++ or an application in C++ or in C# to do that.

More about doing this in C++:

change volume win32 c++

魂归处 2024-08-17 19:55:09

查看 javax.sound API。 这里有一个关于此的教程,特别是此处(在章节更改线路音量) 您可以阅读如何设置音量。这些知识应该能够提供足够的 Google 关键字来查找示例。

Take a look in javax.sound API. Here's a tutorial about that, specifically here (in chapter Changing a Line's Volume) you can read how to set the volume. This knowledge should give enough Google keywords to find examples.

狠疯拽 2024-08-17 19:55:09

我使用以下代码来模拟音量调整:

Robot robot;             // Set speaker volume to 80
try
{
  robot=new Robot();
  robot.mouseMove(1828,1178);
  robot.mousePress(InputEvent.BUTTON1_MASK);
  robot.mouseRelease(InputEvent.BUTTON1_MASK);
  robot.delay(90);
  robot.mouseMove(1828,906);
  robot.mousePress(InputEvent.BUTTON1_MASK);
  robot.mouseRelease(InputEvent.BUTTON1_MASK);
  robot.delay(260);
  robot.mousePress(InputEvent.BUTTON1_MASK);
  robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
catch (AWTException ex)
{
  System.err.println("Can't start Robot: " + ex);
  System.exit(0);
}

并且它有效!

I used the following code to simulate a volume adjustment :

Robot robot;             // Set speaker volume to 80
try
{
  robot=new Robot();
  robot.mouseMove(1828,1178);
  robot.mousePress(InputEvent.BUTTON1_MASK);
  robot.mouseRelease(InputEvent.BUTTON1_MASK);
  robot.delay(90);
  robot.mouseMove(1828,906);
  robot.mousePress(InputEvent.BUTTON1_MASK);
  robot.mouseRelease(InputEvent.BUTTON1_MASK);
  robot.delay(260);
  robot.mousePress(InputEvent.BUTTON1_MASK);
  robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
catch (AWTException ex)
{
  System.err.println("Can't start Robot: " + ex);
  System.exit(0);
}

And it worked !

放飞的风筝 2024-08-17 19:55:09

单击鼠标打开音量控制后,如果相对调整适合您,您还可以使用向上/向下键命令调整音量。

import java.awt.event.KeyEvent;    
robot.keyPress(KeyEvent.VK_UP);
robot.delay(200);
robot.keyRelease(KeyEvent.VK_DOWN);

After opening the volume control with the mouse click you may also adjust volume with key up/down commands, if relative adjustment is appropriate for you.

import java.awt.event.KeyEvent;    
robot.keyPress(KeyEvent.VK_UP);
robot.delay(200);
robot.keyRelease(KeyEvent.VK_DOWN);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文