C#UDP控制电脑系统音量

using System;
using System.Data;
using System.Net;
using System.Net.Sockets;
using NAudio.CoreAudioApi;

namespace VolumeControlApp
{
    class Program
    {
        private static int UDP_PORT = 8123;
        private static UdpClient udpClient;
        private static MMDeviceEnumerator deviceEnumerator = new MMDeviceEnumerator();
        private static MMDevice defaultDevice;

        static void Main(string[] args)
        {
            // 获取默认音频设备
            defaultDevice = deviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);

            // 创建和启动UDP监听
            udpClient = new UdpClient(UDP_PORT);
            udpClient.BeginReceive(ReceiveCallback, null);

            // 最小化到任务栏
            SetWindowState();

            Console.WriteLine("Volume Control App is running...");
            Console.ReadLine();

            // 关闭UDP监听并释放资源
            udpClient.Close();
            udpClient.Dispose();
        }

        private static void ReceiveCallback(IAsyncResult ar)
        {
            IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, UDP_PORT);
            byte[] receiveBytes = udpClient.EndReceive(ar, ref remoteEndPoint);
            string command = System.Text.Encoding.UTF8.GetString(receiveBytes);

            // 根据接收到的命令控制音量
            if (command.Trim().ToLower() == "a")
            {
                AdjustVolumeBy(10);
            }
            else if (command.Trim().ToLower() == "d")
            {
                AdjustVolumeBy(-10);
            }

            // 继续监听
            udpClient.BeginReceive(ReceiveCallback, null);
        }

        private static void AdjustVolumeBy(int increment)
        {
            // 获取当前音量
            float currentVolume = defaultDevice.AudioEndpointVolume.MasterVolumeLevelScalar;

            // 计算新的音量值
            float newVolume = Math.Min(Math.Max(currentVolume + increment / 100f, 0f), 1f);

            // 设置新的音量
            defaultDevice.AudioEndpointVolume.MasterVolumeLevelScalar = newVolume;

            Console.WriteLine("Volume adjusted. New volume: " + newVolume);
        }

        public static void SetWindowState()
        {
            var handle = NativeMethods.GetConsoleWindow();

            if (handle != IntPtr.Zero)
            {
                NativeMethods.ShowWindow(handle, NativeMethods.SW_MINIMIZE);
            }
        }

        private static class NativeMethods
        {
            public const int SW_MINIMIZE = 6;

            [System.Runtime.InteropServices.DllImport("kernel32.dll")]
            public static extern IntPtr GetConsoleWindow();

            [System.Runtime.InteropServices.DllImport("user32.dll")]
            public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        }
    }
}

需要引入NAudio包,VS中安装即可,a,d为命令,端口自己修改,默认音量步长为10

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值