如何以编程方式设置系统卷?

Pae*_*dow 49 .net c# audio

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

Vma*_*man 67

我参加派对有点晚了,但如果你现在正在寻找一个可用的nuget包(AudioSwitcher.AudioApi.CoreAudio),它可以简化音频交互.安装它然后就像这样简单:

CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice;
Debug.WriteLine("Current Volume:" + defaultPlaybackDevice.Volume);
defaultPlaybackDevice.Volume = 80;
Run Code Online (Sandbox Code Playgroud)

  • 如果可以的话,我会两次投票! (7认同)
  • 上面提到了确切的包(AudioSwitcher.AudioApi.CoreAudio).因此,在软件包管理器控制台中,它的"Install-Package AudioSwitcher.AudioApi.CoreAudio".有关详细信息,请访问https://www.nuget.org/packages/AudioSwitcher.AudioApi.CoreAudio/ (3认同)
  • AudioSwitcher.AudioApi.CoreAudio.dll 是一个废弃的错误混乱的地方 (2认同)

Pae*_*dow 49

这是代码:

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

发现在dotnetcurry上

使用WPF时,您需要使用new WindowInteropHelper(this).Handle而不是this.Handle (感谢Alex Beals)

  • 代码示例为+ 1,而其他答案则延迟重定向到教程. (15认同)
  • @Kristof:这不是关于问题应该得到什么,而是社区应该得到的东西,谷歌在遇到同样的问题时会重定向 (13认同)
  • 当链接引用的网站被删除时,答案也就消失了......所以当我将源码复制并粘贴到我的答案中时,它将永远在这里 (5认同)
  • 一个明确没有谷歌搜索的1行问题不应该比懒惰的重定向更多... (3认同)
  • 而不是`this.Handle`,使用`new WindowInteropHelper(this).Handle`. (3认同)

Cas*_*roy 14

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

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

用法:

keybd_event((byte)Keys.VolumeUp, 0, 0, 0); // increase volume
keybd_event((byte)Keys.VolumeDown, 0, 0, 0); // decrease volume
Run Code Online (Sandbox Code Playgroud)

  • 但是如何将音量设置为50%? (10认同)

egf*_*nor 12

如果您希望使用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;
        }
}
Run Code Online (Sandbox Code Playgroud)

  • 对于尝试查找此API的任何人,他们都移至GitHub https://github.com/filoe/cscore,至此注释,请参考“使用CSCore.CoreAudioAPI;”。 (2认同)