代码拉取完成,页面将自动刷新
package org.example.uitls;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
/**
* 每个 Java 应用程序都有一个 java.lang.Runtime 类实例,使应用程序能够与其运行的环境相连接。
*
* @author wangMaoXiong
* @version 1.0
* @date 2023/11/14 9:01
* @see org.example.se.lang.RuntimeTest
*/
public class RuntimeUtil {
private static final Logger log = LoggerFactory.getLogger(RuntimeUtil.class);
/**
* 控制电脑系统音量
* <p/>
* 约定在应用根目录下的 temp 目录中放置3个vbs文件
* volumeMute.vbs:用于静音
* volumeAdd.vbs:增加音量
* volumeMinus.vbs:减小音量
* 文件以及文件的内容采用 Java 代码动态生成,不存在时则新建,存在时则直接调用
*
* @param type 0:静音/取消静音 1:增加音量 2:减小音量
*/
private static void controlSystemVolume(String type) {
try {
if (type == null || "".equals(type.trim())) {
log.info("type 参数为空,不进行操作...");
}
/**tempFile:vbs 文件
* vbsMessage:vbs 文件的内容*/
String vbsMessage = "";
File tempFile = null;
Runtime runtime = Runtime.getRuntime();
switch (type) {
case "0":
tempFile = new File("temp", "volumeMute.vbs");
vbsMessage = !tempFile.exists() ? "CreateObject(\"Wscript.Shell\").Sendkeys \"棴\"" : "";
break;
case "1":
tempFile = new File("temp", "volumeAdd.vbs");
vbsMessage = !tempFile.exists() ? "CreateObject(\"Wscript.Shell\").Sendkeys \"棷\"" : "";
break;
case "2":
tempFile = new File("temp", "volumeMinus.vbs");
vbsMessage = !tempFile.exists() ? "CreateObject(\"Wscript.Shell\").Sendkeys \"棶\"" : "";
break;
default:
return;
}
/**
* 当3个vbs文件不存在时,则创建它们,应用默认编码为 utf-8 时,创建的 vbs 脚本运行时报错
* 于是使用 OutputStreamWriter 将 vbs 文件编码改成gbd就正常了
*/
if (!tempFile.exists() && !vbsMessage.equals("")) {
if (!tempFile.getParentFile().exists()) {
tempFile.getParentFile().mkdirs();
}
tempFile.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "GBK");
outputStreamWriter.write(vbsMessage);
outputStreamWriter.flush();
outputStreamWriter.close();
log.info("vbs 文件不存在,新建成功:" + tempFile.getAbsolutePath());
}
runtime.exec("wscript " + tempFile.getAbsolutePath()).waitFor();
log.info("音量控制完成.");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
log.info("1 秒后开始静音.");
Thread.sleep(1000);
controlSystemVolume("0");
log.info("1 秒后开始取消静音.");
Thread.sleep(1000);
controlSystemVolume("0");
log.info("1 秒后开始增加 2 格音量,可以使用循环,持续增加音量.");
Thread.sleep(1000);
controlSystemVolume("1");
log.info("1 秒后开始减小音量,可以使用循环持续减小.");
Thread.sleep(1000);
for (int i = 0; i < 3; i++) {
controlSystemVolume("2");
Thread.sleep(500);
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。