Java 控制系统音量 及 音视频播放

本文导读

  • 使用Java来控制Windows系统音量,使用JNA调用windows底层API因为有点麻烦,所以这里采用VBS控制的方式
  • 可以参考《VBS 控制 Windos 系统音量 及视频播放》,本文同样是利用VBS来控制,区别在于这里的vbs文件会用Java代码动态生成,灵活性更强

音量控制

源代码

package com.lct.utils;

import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.WinNT;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

/**
 * Created by Administrator on 2018/6/26 0026.
 * 系统工具类
 */
public class SystemUtils {
    /**
     * 控制系统音量
     * 约定在应用根目录下的temp目录中放置3vbs文件
     * volumeMute.vbs:用于静音
     * volumeAdd.vbs:增加音量
     * volumeMinus.vbs:减小音量
     * 文件以及文件的内容采用Java代码动态生成
     * 不存在时则新建,存在时则字节调用
     *
     * @param type 0:静音/取消静音    1:增加音量  2:减小音量
     */
    public static void controlSystemVolume(String type) {
        try {
            if (StringUtils.isBlank(type)) {
                return;
            }
            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;
            }
            /**
             * 3vbs文件不存在时,则创建它们,应用默认编码为utf-8时,创建的vbs脚本运行时报错
             * 于是使用OutputStreamWritervbs文件编码改成gbd就正常了
             */
            if (!tempFile.exists() && StringUtils.isNotBlank(vbsMessage)) {
                if (!tempFile.getParentFile().exists()) {
                    tempFile.getParentFile().mkdirs();
                }
                System.out.println(tempFile.getAbsolutePath());
                tempFile.createNewFile();
                FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "GBK");
                outputStreamWriter.write(vbsMessage);
                outputStreamWriter.flush();
                outputStreamWriter.close();
            }
            runtime.exec("wscript " + tempFile.getAbsolutePath()).waitFor();
            LogWmxUtils.writeLine("System volume control:" + tempFile.getName());
        } catch (IOException e) {
            e.printStackTrace();
            LogWmxUtils.writeLine("xxxException :" + e.getMessage());
        } catch (InterruptedException e) {
            e.printStackTrace();
            LogWmxUtils.writeLine("xxxException :" + e.getMessage());
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread.sleep(3000);
        controlSystemVolume("2");
    }
}

运行结果

播放 暂停

源代码


运行结果



猜你喜欢

转载自blog.csdn.net/wangmx1993328/article/details/80856949
今日推荐