首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

C# 控制麥克風靜音/音量:Windows Core Audio API (MMDeviceAPI) 完整實作指南

     編輯:程式設計綜合問答

為何不直接用 SendMessage?

  • SendMessage(WM_APPCOMMAND, APPCOMMAND_MICROPHONE_VOLUME_MUTE) 僅模擬鍵盤多媒體鍵,**依賴殼層處理**,不可靠且無法精確控制特定裝置。
  • 現代 Windows (Vista+) 採用 Core Audio API (MMDeviceAPI),提供裝置級、會話級、端點級精細音量控制。

核心介面與流程

  1. IMMDeviceEnumerator → 列舉音訊端點。
  2. IMMDevice → 代表特定麥克風端點。
  3. IAudioEndpointVolume → 控制主音量、靜音、分頻道音量。
  4. ISimpleAudioVolume (會話級) → 控制單一應用程式音量。

完整 C# 封裝類別 (需參考 Microsoft.WindowsAPICodePack.Shell 或手動 P/Invoke)

using System;
using System.Runtime.InteropServices;

public class MicrophoneController {
    // 介面 GUID
    private static readonly Guid IID_IAudioEndpointVolume = new Guid("5CDF2C82-841E-4546-9722-0CF74078229A");
    
    [ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")]
    private class MMDeviceEnumerator { }

    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("A95664D2-9614-4F35-A746-DE8DB63617E6")]
    private interface IMMDeviceEnumerator {
        int EnumAudioEndpoints(int dataFlow, int dwStateMask, out IMMDeviceCollection ppDevices);
        int GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice ppEndpoint);
    }

    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("D666063F-1587-4E43-81F1-B948E807363F")]
    private interface IMMDevice {
        int Activate(ref Guid iid, int dwClsCtx, IntPtr pActivationParams, out IntPtr ppInterface);
    }

    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("5CDF2C82-841E-4546-9722-0CF74078229A")]
    private interface IAudioEndpointVolume {
        int GetMute(out bool pbMute);
        int SetMute(bool bMute);
        int GetMasterVolumeLevelScalar(out float pfLevel);
        int SetMasterVolumeLevelScalar(float fLevel, ref Guid pguidEventContext);
        // 其餘方法省略...
    }

    public static void SetMute(bool mute) {
        var enumerator = (IMMDeviceEnumerator)new MMDeviceEnumerator();
        enumerator.GetDefaultAudioEndpoint(1, 1, out IMMDevice device); // eCapture=1, eConsole=1
        device.Activate(ref IID_IAudioEndpointVolume, 1, IntPtr.Zero, out IntPtr ptr);
        var volume = (IAudioEndpointVolume)Marshal.GetTypedObjectForIUnknown(ptr);
        volume.SetMute(mute);
        Marshal.Release(ptr);
    }

    public static void SetVolume(float level) { // 0.0 ~ 1.0
        // 同理取得 volume 介面
        // volume.SetMasterVolumeLevelScalar(level, ref Guid.Empty);
    }
}

更簡便方案:NAudio 庫

// NuGet: Install-Package NAudio
using NAudio.CoreAudioApi;

var enumerator = new MMDeviceEnumerator();
var device = enumerator.GetDefaultAudioEndpoint(DataFlow.Capture, Role.Console);
device.AudioEndpointVolume.Mute = true;   // 靜音
device.AudioEndpointVolume.MasterVolumeLevelScalar = 0.5f; // 50%

關鍵常數

常數值含義
DataFlow.Capture1輸入裝置 (麥克風)
Role.Console1主控台角色 (預設通訊裝置)
MasterVolumeLevelScalar0.0~1.0標準化音量 (不受分貝影響)

權限與部署注意

  • 應用程式需在 app.manifest 宣告 。
  • 若針對特定應用程式控制音量,改用 IAudioSessionControl2 + ISimpleAudioVolume (會話級 API)。
  • 測試時先在「聲音控制面板 → 錄音」確認目標麥克風為「預設裝置」。

採用 NAudio 封裝 Core Audio API,幾行代碼即可實現專業級麥克風靜音/音量控制,避開 SendMessage 不穩定與相容性坑。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved