使用C#静音Windows音量

18

有人知道如何使用C#编程来静音Windows XP的音量吗?


在Vista/Win7中是否可能? - meir
在Vista及以上版本中,您必须使用IAudioEndpointVolume。 - Norbert Willhelm
5个回答

16

声明此内容以进行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);

然后使用这行代码来静音/取消静音声音。

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

2
在XP上运行得很好,谢谢。但是我正在使用WPF,所以没有this.Handle。相反:public static void ToggleMute(IntPtr handle) { SendMessageW(handle, WM_APPCOMMAND, handle, (IntPtr)APPCOMMAND_VOLUME_MUTE); }在WPF窗口中: VolumeXP.ToggleMute(new WindowInteropHelper(this).Handle); - Stephen Kennedy
1
这个程序在Windows 7上运行良好,但我现在在一个后台运行的库中,而不是任何将拥有窗口的东西。我正在尝试静音另一个窗口,这个方法可以实现,但无论我传递什么句柄(另一个应用程序的窗口句柄等),它都只会静音主系统音量... - BrainSlugs83
为什么要使用 0x80000APPCOMMAND_VOLUME_MUTE 被定义为 8,但是当我使用 8 时它会失败,但是当我使用你的 0x80000 时它可以工作。这很奇怪。 - Noitidart
这里提供了关于为什么会出现0x80000的解决方案 - https://dev59.com/f5zha4cB1Zd3GeqPFXRK#40577881 - Noitidart
很好!你可以使用APPCOMMAND_VOLUME_UP 0xA0000来确保不像闹钟一样静音。 - colin lamarre

6
你可以使用NAudio来处理Windows Vista/7甚至可能是8的相关问题:NAudio,下载最新版本并提取DLL文件,在C#项目中引用NAudio DLL文件.代码如下,可遍历所有可用音频设备并在可能的情况下将其静音。
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
}

谢谢。这个库在其他所有我尝试过的库都失败的情况下起作用了。我的问题与这个类似,但不完全相同。这是一个非常好的发现。我希望我能给你投多次票。 - David
感谢您的点赞和反馈。在找到一个简单的方法来静音之前,我搜索了一段时间。因此,我想在一些stackoverflow问题上分享这个知识 :) 我很高兴它能帮助到某人。 - Mike de Klerk

2

请参考如何使用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;
            }
        }
    }
}

0

这是Mike de Klerks答案的稍微改进版本,不需要“on error resume next”代码。

步骤1:将NAudio NuGet包添加到您的项目中(https://www.nuget.org/packages/NAudio/

步骤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;
        }
    }
}

-1
CoreAudioDevice defaultPlaybackDevice = new 
CoreAudioController().DefaultPlaybackDevice;   
        if (!defaultPlaybackDevice.IsMuted)
            defaultPlaybackDevice.ToggleMute();

CoreAudioDevice 是从哪里来的? - BDL

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接