17

任何人都知道如何使用 C# 以编程方式使 Windows XP Volume 静音?

4

5 回答 5

15

为 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);
于 2008-09-30T17:35:18.413 回答
6

您可以在 Windows Vista/7 和 8 中使用什么:

您可以使用NAudio
下载最新版本。提取 DLL 并在 C# 项目中引用 DLL NAudio。

然后添加以下代码以遍历所有可用的音频设备并尽可能将其静音。

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
}
于 2012-09-21T16:33:24.167 回答
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;
            }
        }
    }
}
于 2014-02-02T12:48:46.787 回答
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;
        }
    }
}
于 2020-10-18T11:34:25.360 回答
-1
CoreAudioDevice defaultPlaybackDevice = new 
CoreAudioController().DefaultPlaybackDevice;   
        if (!defaultPlaybackDevice.IsMuted)
            defaultPlaybackDevice.ToggleMute();
于 2022-02-03T12:10:09.607 回答