17

C# を使用して Windows XP のボリュームをプログラムでミュートする方法を知っている人はいますか?

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 に答える
0

これは、Mike de Klerks の回答をわずかに改善したバージョンで、「次のエラーで再開」コードを必要としません。

ステップ 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 に答える