60

如何使用 C# 应用程序更改 Windows 系统音量?

4

6 回答 6

88

我参加聚会有点晚了,但如果您现在正在寻找一个可用的 nuget 包(AudioSwitcher.AudioApi.CoreAudio),它可以简化音频交互。安装它然后它很简单:

CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice;
Debug.WriteLine("Current Volume:" + defaultPlaybackDevice.Volume);
defaultPlaybackDevice.Volume = 80;
于 2016-11-01T14:18:10.320 回答
54

Here is the code:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Test
{
    public class Test
    {
        private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
        private const int APPCOMMAND_VOLUME_UP = 0xA0000;
        private const int APPCOMMAND_VOLUME_DOWN = 0x90000;
        private const int WM_APPCOMMAND = 0x319;

        [DllImport("user32.dll")]
        public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg,
            IntPtr wParam, IntPtr lParam);

        private void Mute()
        {
            SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
                (IntPtr)APPCOMMAND_VOLUME_MUTE);
        }

        private void VolDown()
        {
            SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
                (IntPtr)APPCOMMAND_VOLUME_DOWN);
        }

        private void VolUp()
        {
            SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
                (IntPtr)APPCOMMAND_VOLUME_UP);
        }
    }
}

Found on dotnetcurry

When using WPF you need to use new WindowInteropHelper(this).Handle instead of this.Handle (thanks Alex Beals)

于 2012-10-30T12:56:35.490 回答
15

如果其他答案中提供的教程过于复杂,您可以使用keybd_event 函数尝试这样的实现

[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);

用法:

keybd_event((byte)Keys.VolumeUp, 0, 0, 0); // increase volume
keybd_event((byte)Keys.VolumeDown, 0, 0, 0); // decrease volume
于 2012-10-30T12:55:40.063 回答
14

如果您希望使用 Core Audio API 将其设置为精确值:

using CoreAudioApi;

public class SystemVolumeConfigurator
{
        private readonly MMDeviceEnumerator _deviceEnumerator = new MMDeviceEnumerator();
        private readonly MMDevice _playbackDevice;

        public SystemVolumeConfigurator()
        {
            _playbackDevice = _deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
        }

        public int GetVolume()
        {
            return (int)(_playbackDevice.AudioEndpointVolume.MasterVolumeLevelScalar * 100);
        }

        public void SetVolume(int volumeLevel)
        {
            if (volumeLevel < 0 || volumeLevel > 100)
                throw new ArgumentException("Volume must be between 0 and 100!");

            _playbackDevice.AudioEndpointVolume.MasterVolumeLevelScalar = volumeLevel / 100.0f;
        }
}
于 2016-02-26T19:40:45.267 回答
1

您可以将此库https://gist.github.com/sverrirs/d099b34b7f72bb4fb386添加到您的项目中并像这样更改音量;

VideoPlayerController.AudioManager.SetMasterVolume(100);

该库还包括更改应用程序音量、静音、获取当前音量级别等选项。命名空间称为“视频播放器控制器”,但我在 Windows 窗体应用程序中使用它来更改系统音量,它工作正常,所以“视频" 部分是任意的。

于 2020-08-22T09:17:35.963 回答
0

我的代码有点不同,但仍在使用 CoreAudio

下载了 pkg:nuget install AudioSwitcher.AudioApi.CoreAudio -Version 3.0.0.1

using AudioSwitcher.AudioApi.CoreAudio;
public partial class MainWindow : Window
{
public MainWindow()
{

InitializeComponent();

CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice;

double vol = defaultPlaybackDevice.Volume;

defaultPlaybackDevice.Volume = defaultPlaybackDevice.Volume - 5.0;

defaultPlaybackDevice.Volume = defaultPlaybackDevice.Volume + 5.0;
}
}
于 2018-08-18T13:39:06.980 回答