java 设置电脑音量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4964947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:49:11  来源:igfitidea点击:

Set Computer Volume

javavolume

提问by Caleb Guidos

I want to make a java program that

我想做一个java程序

  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
  1. 从文件中读取一行(例如文件文本为:“Hello”)
  2. 如果 Line = "Hello" 那么
  3. 如果计算机音量 > 1,则获取计算机音量,则 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)

我忘了添加(我使用的是 Windows 7)

回答by Witek

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

源代码在这里:https: //github.com/Kunagi/ilarkesto/blob/master/src/main/java/ilarkesto/media/Audio.java

You can copy and use the code.

您可以复制和使用代码。

回答by Just_Paul

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:

我知道这个问题很老,但也许我的回答对其他人有帮助。我找到了一个有用的工具(下载链接在页面底部)。使用此工具,您可以调整系统音量并执行许多其他操作(例如打开 CD 驱动器)。只需下载它并将 nircmd.exe 放在硬盘上的某个位置。然后编写以下内容来调整您的音量:

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)

这会将您的系统音量设置为 50。要设置您的音量,您需要在 0 到 65535 之间选择一个数字。如果您想使用 0 到 100 之间的数字,请使用以下方法。它将转换您想要的音量(通过使用简单的数学: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,

注意:遗憾的是,这只适用于 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

“因为 Java 是跨平台的,它不能做特定于平台的事情,比如改变音量或任何你想做的事情来控制操作系统。你需要使用操作系统独特的 API 层来做到这一点。” 来源

回答by Michael Sims

Here is how I successfully set the systems master volume using a terminal command (I am using a Mac running OS X 10.10):

以下是我使用终端命令成功设置系统主卷的方法(我使用的是运行 OS X 10.10 的 Mac):

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:

value 可以是 .1 到 7.0 之间的任何值,您可以像这样调用方法:

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?

这将使系统音量大约为最大值的一半。我不知道在 Windows 中设置系统音量的任何命令行选项,如果有人知道,也许他们可以加入?