【问题标题】:How to programmatically set the system volume?如何以编程方式设置系统音量?
【发布时间】:2012-10-19 19:17:11
【问题描述】:

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

【问题讨论】:

  • 为什么要删除重复的链接? Core Audio API(控制混音器)不只是调整单个程序的音量,它也是您调整系统音量的方式。去实际阅读副本中的答案,并按照他们的链接访问各种 MSDN 文章。
  • 但是副本中的答案并没有提供最简单的解决方案。相反,它建议使用不必要的库。由于我的具体问题可以通过内置方法轻松解决,因此这不是链接为重复的问题的重复(尽管它非常相似)。

标签: c# .net audio


【解决方案1】:

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

CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice;
Debug.WriteLine("Current Volume:" + defaultPlaybackDevice.Volume);
defaultPlaybackDevice.Volume = 80;

【讨论】:

  • 上面提到了确切的包(AudioSwitcher.AudioApi.CoreAudio)。因此,在包管理器控制台中,它的“Install-Package AudioSwitcher.AudioApi.CoreAudio”。更多详情nuget.org/packages/AudioSwitcher.AudioApi.CoreAudio
  • 由于某种原因,如果我在使用此方法时得到A first chance exception of type 'System.NullReferenceException' occurred in AudioSwitcher.AudioApi.CoreAudio.dll 的连续循环。它并没有阻止它工作,但有没有办法消除这个异常?
  • 不能与 .NET Core 一起使用。
  • 这应该是公认的解决方案,而不是那些“编写 gui 脚本”、类似 autoit 的东西...
  • AudioSwitcher.AudioApi.CoreAudio.dll 是一个废弃的马车混乱
【解决方案2】:

代码如下:

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);
        }
    }
}

发现于dotnetcurry

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

【讨论】:

  • 我正在尝试使用 C# 在 WPF 应用程序中使用此代码,我在 SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle, (IntPtr)APPCOMMAND_VOLUME_MUTE); --- This.handle 在 WPF 中不可用。你能查一下吗?
  • 也许您可以尝试使用下面的答案。它不应该干扰 WPF 的限制。 (或者使用 WinForms。它可能更旧但仍然好很多(恕我直言))
  • new WindowInteropHelper(this).Handle代替this.Handle
  • 为什么它在我的 WinForms 应用程序中不起作用?我错过了什么吗?
  • 我们的 WinForms 应用程序在没有运行 Windows 资源管理器的 PC 上运行。如果没有运行 Windows 资源管理器,这将不起作用。我还不知道解决方法。
【解决方案3】:

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

[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

【讨论】:

  • 但是如何将音量设置为50%?
  • 没有比这更简单的了,可能会注意到“keys”枚举需要 System.Windows.Forms,但命令不需要。 voldown = 174,volup = 175,volmute = 173
  • @CasterTroy。这对于没有 Windows 窗体的简单用例非常有用。这种方法为任何 nuget 包提供了零依赖。花了几个小时学习和尝试其他方法,然后又回到了这个。赞!非常感谢这个超级智能的解决方案!
  • 而且它似乎与 Windows 2000 兼容。我不得不谷歌搜索这个版本。它是 Win 98 之后的版本和 Win XP 之前的一个版本。与其他音频控制实现不同,对于仍在使用 Win XP 的家庭用户来说,这是一个很好的兼容性功能。
【解决方案4】:

如果您希望使用 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;
        }
}

【讨论】:

【解决方案5】:

你可以把这个库https://gist.github.com/sverrirs/d099b34b7f72bb4fb386添加到你的项目中,像这样改变音量;

VideoPlayerController.AudioManager.SetMasterVolume(100);

该库还包括更改应用程序音量、静音、获取当前音量级别等选项。 该命名空间称为“视频播放器控制器”,但我在 Windows 窗体应用程序中使用它来更改系统音量,并且效果很好,因此“视频”部分是任意的。

【讨论】:

  • 我发现这个答案最有帮助,因为它设置了当前设备音量,而不仅仅是默认设备音量。这意味着您可以切换播放设备,并且仍然可以控制音量,而无需采取任何额外的步骤。当然,直接使用CoreAudio也可以设置当前设备的音量,但是要找到当前的播放设备需要额外的步骤。归根结底,这个解决方案像其他解决方案一样使用 CoreAudio,但将其包装在一个非常方便的包中,所有这些都包含在一个文件中。简洁明了。
【解决方案6】:

我的代码有点不同,但仍在使用 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;
}
}

【讨论】:

  • 为什么这个库这么慢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-16
  • 2018-05-06
  • 2018-02-25
相关资源
最近更新 更多