简体   繁体   English

使用 C# 静音 Windows 音量

[英]Mute Windows Volume using C#

Anyone know how to programmatically mute the Windows XP Volume using C#?有人知道如何使用 C# 以编程方式使 Windows XP Volume 静音吗?

Declare this for P/Invoke:为 P/Invoke 声明:

private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
private const int WM_APPCOMMAND = 0x319;

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

And then use this line to mute/unmute the sound.然后使用此行将声音静音/取消静音。

SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle, (IntPtr) APPCOMMAND_VOLUME_MUTE);

What you can use for Windows Vista/7 and probably 8 too:你可以在 Windows Vista/7 和 8 上使用什么:

You can use NAudio .您可以使用NAudio
Download the latest version.下载最新版本。 Extract the DLLs and reference the DLL NAudio in your C# project.提取 DLL 并在 C# 项目中引用 DLL NAudio。

Then add the following code to iterate through all available audio devices and mute it if possible.然后添加以下代码以遍历所有可用的音频设备并尽可能将其静音。

try
{
    //Instantiate an Enumerator to find audio devices
    NAudio.CoreAudioApi.MMDeviceEnumerator MMDE = new NAudio.CoreAudioApi.MMDeviceEnumerator();
    //Get all the devices, no matter what condition or status
    NAudio.CoreAudioApi.MMDeviceCollection DevCol = MMDE.EnumerateAudioEndPoints(NAudio.CoreAudioApi.DataFlow.All, NAudio.CoreAudioApi.DeviceState.All);
    //Loop through all devices
    foreach (NAudio.CoreAudioApi.MMDevice dev in DevCol)
    {
        try
        {
            //Show us the human understandable name of the device
            System.Diagnostics.Debug.Print(dev.FriendlyName);
            //Mute it
            dev.AudioEndpointVolume.Mute = true;
        }
        catch (Exception ex)
        {
            //Do something with exception when an audio endpoint could not be muted
        }
    }
}
catch (Exception ex)
{
    //When something happend that prevent us to iterate through the devices
}

如果您运行的是 Vista,我遇到了这个可能会感兴趣的项目

See How to programmatically mute the Windows XP Volume using C#?请参阅如何使用 C# 以编程方式将 Windows XP 卷静音?

void SetPlayerMute(int playerMixerNo, bool value)
{
    Mixer mx = new Mixer();
    mx.MixerNo = playerMixerNo;
    DestinationLine dl = mx.GetDestination(Mixer.Playback);
    if (dl != null)
    {
        foreach (MixerControl ctrl in dl.Controls)
        {
            if (ctrl is MixerMuteControl)
            {
                ((MixerMuteControl)ctrl).Value = (value) ? 1 : 0;
                break;
            }
        }
    }
}

You will probably want to use MCI commands: http://msdn.microsoft.com/en-us/library/ms709461(VS.85).aspx您可能想要使用 MCI 命令: http : //msdn.microsoft.com/en-us/library/ms709461(VS.85).aspx

I should add that while this will give you good general control over the input and output mixers in windows, you may have some difficulty with doing detailed controls, like setting the mic boost, etc.我应该补充一点,虽然这可以让您对 Windows 中的输入和输出混音器进行良好的总体控制,但您可能会在进行详细控制时遇到一些困难,例如设置麦克风增强等。

Oh, and if you're on Vista, then just forget it.哦,如果您使用的是 Vista,那就忘记它吧。 It's a totally different model.这是一个完全不同的模型。

You could use P/Invoke as explained here: http://www.microsoft.com/indonesia/msdn/pinvoke.aspx .您可以按照此处的说明使用 P/Invoke: http : //www.microsoft.com/indonesia/msdn/pinvoke.aspx It actually goes through the steps in Task 1: Mute and Unmute Sound near the top.它实际上经历了任务 1:顶部附近的静音和取消静音中的步骤。

This is a slightly improved version of Mike de Klerks answer that doesn't require "on error resume next" code.这是 Mike de Klerks 答案的略微改进版本,不需要“下一个错误恢复”代码。

Step 1: Add the NAudio NuGet-package to your project ( https://www.nuget.org/packages/NAudio/ )步骤 1:将 NAudio NuGet 包添加到您的项目 ( https://www.nuget.org/packages/NAudio/ )

Step 2: Use this code:第 2 步:使用此代码:

using (var enumerator = new NAudio.CoreAudioApi.MMDeviceEnumerator())
{
    foreach (var device in enumerator.EnumerateAudioEndPoints(NAudio.CoreAudioApi.DataFlow.Render, NAudio.CoreAudioApi.DeviceState.Active))
    {
        if (device.AudioEndpointVolume?.HardwareSupport.HasFlag(NAudio.CoreAudioApi.EEndpointHardwareSupport.Mute) == true)
        {
            Console.WriteLine(device.FriendlyName);
            device.AudioEndpointVolume.Mute = false;
        }
    }
}
CoreAudioDevice defaultPlaybackDevice = new 
CoreAudioController().DefaultPlaybackDevice;   
        if (!defaultPlaybackDevice.IsMuted)
            defaultPlaybackDevice.ToggleMute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM