如何检查系统音频是否静音?

问题描述 投票:0回答:2

我发现这个答案建议使用“core audio.dll”API和以下代码:

private function getmute() as boolean
    Dim devenum As New MMDeviceEnumerator
    Dim device As MMDevice = devenum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia)
    If device.AudioEndpointVolume.Mute = True Then
        Return True
    Else
        Return False
    End If
End Function

我尝试了该代码,但它不起作用,而且我无法对其他问题发表评论。

当我尝试上面的代码时,以下行抛出异常:

If device.AudioEndpointVolume.Mute = True Then

这是异常消息:

AudioEndpointVolume 不是设备的成员。

我已经导入了

CoreAudioAPI
,还有什么问题吗?

我正在寻找

VB.NET
C#
解决方案。

c# .net vb.net winapi core-audio
2个回答
9
投票

VB.Net
重写(+奖励)
CoreAudio API
实施 γηράσκω δ' αεί πολλά διδασκόμε 已发布。

代码封装在

Class
(
CoreAudio
) 中,包括原始
IsMuted()
方法,以及我即时实现的其他方法(因为,为什么不呢)。

有关 MSDN 上的 Windows CoreAudio API 的更多信息。
GitHub NAudio CoreAudioApi 实现 (

C#
)。

实施方法:

IsMuted() As Integer

返回默认音频设备的静音状态。

SetMute(Boolean)

设置默认音频设备的静音状态 (
True/False
)

IsMediadPlaying()
默认音频设备当前是否正在播放

GetVolume() As Integer

获取默认音频设备的音量状态。值范围为
0 to 100

SetVolume(Integer)

将默认音频设备的音量状态设置为
0 to 100
范围内的值

VolumeUp()

将默认音频设备的音量提高一级(通常为 2 个单位)

VolumeDown()

将默认音频设备的音量降低一级。

用途:

 Dim isMuted As Boolean = AudioDevice.IsMuted()

 ' True=Mutes the Audio, False=Un-Mutes (?) it
 AudioDevice.SetMute(True)

 ' Checks whether the Media Device currently playing
 Dim isPlaying As Boolean = AudioDevice.IsMediadPlaying()

 Dim volume As Integer = AudioDevice.GetVolume()

 ' Sets the Audio level to 50%, returns the previous value
 Dim previousVolume As Integer = AudioDevice.SetVolume(50)

 ' Increases the Audio level by 1 Step
 AudioDevice.VolumeUp()

 ' Decreases the Audio level by 1 Step
 AudioDevice.VolumeDown()

声明:

Imports System
Imports System.Runtime.InteropServices

Public Class AudioDevice
    Friend Enum EDataFlow
        eRender
        eCapture
        eAll
        EDataFlow_enum_count
    End Enum

    Friend Enum ERole
        eConsole
        eMultimedia
        eCommunications
        ERole_enum_count
    End Enum

    <Flags>
    Public Enum CLSCTX As UInteger
        CLSCTX_INPROC_SERVER = &H1
        CLSCTX_INPROC_HANDLER = &H2
        CLSCTX_LOCAL_SERVER = &H4
        CLSCTX_INPROC_SERVER16 = &H8
        CLSCTX_REMOTE_SERVER = &H10
        CLSCTX_INPROC_HANDLER16 = &H20
        CLSCTX_RESERVED1 = &H40
        CLSCTX_RESERVED2 = &H80
        CLSCTX_RESERVED3 = &H100
        CLSCTX_RESERVED4 = &H200
        CLSCTX_NO_CODE_DOWNLOAD = &H400
        CLSCTX_RESERVED5 = &H800
        CLSCTX_NO_CUSTOM_MARSHAL = &H1000
        CLSCTX_ENABLE_CODE_DOWNLOAD = &H2000
        CLSCTX_NO_FAILURE_LOG = &H4000
        CLSCTX_DISABLE_AAA = &H8000
        CLSCTX_ENABLE_AAA = &H10000
        CLSCTX_FROM_DEFAULT_CONTEXT = &H20000
        CLSCTX_ACTIVATE_32_BIT_SERVER = &H40000
        CLSCTX_ACTIVATE_64_BIT_SERVER = &H80000
        CLSCTX_INPROC = CLSCTX_INPROC_SERVER Or CLSCTX_INPROC_HANDLER
        CLSCTX_SERVER = CLSCTX_INPROC_SERVER Or CLSCTX_LOCAL_SERVER Or CLSCTX_REMOTE_SERVER
        CLSCTX_ALL = CLSCTX_SERVER Or CLSCTX_INPROC_HANDLER
    End Enum

    <ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")>
    Public Class MMDeviceEnumeratorComObject
    End Class

    <ComImport, Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
    Friend Interface IMMDeviceEnumerator
        Function NotImpl1() As Integer
        Function GetDefaultAudioEndpoint(
              dataFlow As EDataFlow,
              role As ERole,
              <Out> ByRef ppDevice As IMMDevice) As Integer
    End Interface

    <ComImport, Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
    Public Interface IMMDevice
        Function Activate(<MarshalAs(UnmanagedType.LPStruct)> iid As Guid,
                          dwClsCtx As CLSCTX, pActivationParams As IntPtr) As <MarshalAs(UnmanagedType.IUnknown)> Object
        'Function Activate(ByRef iid As Guid, dwClsCtx As CLSCTX, pActivationParams As IntPtr, <Out> ByRef ppInterface As IAudioEndpointVolume) As Integer
    End Interface

    <ComImport, Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
    Public Interface IAudioEndpointVolume
        Function RegisterControlChangeNotify(pNotify As IAudioEndpointVolumeCallback) As Integer
        Function UnregisterControlChangeNotify(pNotify As IAudioEndpointVolumeCallback) As Integer
        Function GetChannelCount(ByRef channelCount As Integer) As Integer
        Function SetMasterVolumeLevel() As Integer
        Function SetMasterVolumeLevelScalar(level As Single, eventContext As Guid) As Integer
        Function GetMasterVolumeLevel(<Out> ByRef level As Single) As Integer
        Function GetMasterVolumeLevelScalar(<Out> ByRef level As Single) As Integer
        Function SetChannelVolumeLevel(channelNumber As Integer, level As Single, eventContext As Guid) As Integer
        Function SetChannelVolumeLevelScalar(channelNumber As Integer, level As Single, eventContext As Guid) As Integer
        Function GetChannelVolumeLevel(channelNumber As Integer, <Out> ByRef level As Single) As Integer
        Function GetChannelVolumeLevelScalar(channelNumber As Integer, <Out> ByRef level As Single) As Integer
        Function SetMute(<MarshalAs(UnmanagedType.Bool)> isMuted As Boolean, eventContext As Guid) As Integer
        Function GetMute(<Out> ByRef isMuted As Boolean) As Integer
        Function GetVolumeStepInfo(<Out> ByRef pnStep As Integer, ByRef pnStepCount As Integer) As Integer
        Function VolumeStepUp(eventContext As Guid) As Integer
        Function VolumeStepDown(eventContext As Guid) As Integer
        Function QueryHardwareSupport(<Out> ByRef hardwareSupportMask As Integer) As Integer
        Function GetVolumeRange(<Out> ByRef volumeMin As Single, <Out> ByRef volumeMax As Single, <Out> ByRef volumeStep As Single) As Integer
    End Interface

    <ComImport, Guid("657804FA-D6AD-4496-8A60-352752AF4F89"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
    Public Interface IAudioEndpointVolumeCallback
        <PreserveSig()>
        Function OnNotify(pNotifyData As IntPtr) As Integer  ' HRESULT
    End Interface

    <InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("C02216F6-8C67-4B5B-9D00-D008E73E0064")>
    Public Interface IAudioMeterInformation
        Function GetPeakValue() As Single
    End Interface

    Public Shared Function IsMuted() As Boolean
        Dim masterVol As IAudioEndpointVolume = GetMasterVolumeObject()
        Try
            If masterVol Is Nothing Then
                Throw New ApplicationException("Master Volume not available")
            End If

            Dim isAudioMuted As Boolean
            masterVol.GetMute(isAudioMuted)
            Return isAudioMuted
        Finally
            If masterVol IsNot Nothing Then
                If masterVol IsNot Nothing Then Marshal.ReleaseComObject(masterVol)
            End If
        End Try
    End Function

    Public Shared Sub SetMute(IsMute As Boolean)
        Dim masterVol As IAudioEndpointVolume = GetMasterVolumeObject()
        Try
            masterVol.SetMute(IsMute, Guid.Empty)
        Finally
            If masterVol IsNot Nothing Then
                If masterVol IsNot Nothing Then Marshal.ReleaseComObject(masterVol)
            End If
        End Try
    End Sub

    Public Shared Function IsMediadPlaying() As Boolean
        Dim mediaDevice As IMMDevice = Nothing
        Try
            mediaDevice = GetDefaultAudioEndpoint()
            Dim meter = mediaDevice.Activate(GetType(IAudioMeterInformation).GUID, CLSCTX.CLSCTX_ALL, IntPtr.Zero)
            Dim value As Single = DirectCast(meter, IAudioMeterInformation).GetPeakValue()
            Return value > 0.00000001F
        Finally
            If mediaDevice IsNot Nothing Then Marshal.ReleaseComObject(mediaDevice)
        End Try
    End Function

    Public Shared Function GetVolume() As Integer
        Dim fVolume As Single
        Dim masterVol As IAudioEndpointVolume = GetMasterVolumeObject()
        Try
            masterVol.GetMasterVolumeLevelScalar(fVolume)
            Return CType(fVolume * 100, Integer)
        Finally
            If masterVol IsNot Nothing Then Marshal.ReleaseComObject(masterVol)
        End Try
    End Function

    Public Shared Function SetVolume(volume As Integer) As Integer
        volume = Math.Max(Math.Min(volume, 100), 0)
        Dim masterVolume As IAudioEndpointVolume = GetMasterVolumeObject()
        Try
            Dim prevVolume = GetVolume()
            masterVolume.SetMasterVolumeLevelScalar(volume / 100.0F, Guid.Empty)
            Return prevVolume
        Finally
            If masterVolume IsNot Nothing Then Marshal.ReleaseComObject(masterVolume)
        End Try
    End Function

    Public Shared Sub VolumeUp()
        Dim masterVol As IAudioEndpointVolume = GetMasterVolumeObject()
        Try
            masterVol.VolumeStepUp(Guid.Empty)
        Finally
            If masterVol IsNot Nothing Then Marshal.ReleaseComObject(masterVol)
        End Try
    End Sub

    Public Shared Sub VolumeDown()
        Dim masterVol As IAudioEndpointVolume = GetMasterVolumeObject()
        Try
            masterVol.VolumeStepDown(Guid.Empty)
        Finally
            If masterVol IsNot Nothing Then Marshal.ReleaseComObject(masterVol)
        End Try
    End Sub

    Friend Shared Function GetMasterVolumeObject() As IAudioEndpointVolume
        Dim mediaDevice As IMMDevice = GetDefaultAudioEndpoint()
        Try
            Dim ppEndpoint = mediaDevice.Activate(GetType(IAudioEndpointVolume).GUID, CLSCTX.CLSCTX_ALL, IntPtr.Zero)
            Return DirectCast(ppEndpoint, IAudioEndpointVolume)
        Finally
            If mediaDevice IsNot Nothing Then Marshal.ReleaseComObject(mediaDevice)
        End Try
    End Function

    Public Shared Function GetDefaultAudioEndpoint() As IMMDevice
        Dim mediaDevice As IMMDevice = Nothing
        Dim deviceEnumerator = DirectCast(New MMDeviceEnumeratorComObject(), IMMDeviceEnumerator)
        Try
            deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, mediaDevice)
            Return mediaDevice
        Finally
            If deviceEnumerator IsNot Nothing Then Marshal.ReleaseComObject(deviceEnumerator)
        End Try
    End Function
End Class

6
投票

使用 C/C++ 并了解一些 directshow 的知识非常简单,但使用 C# 时,您需要某种包装器来进行调用。老实说,我不使用托管语言来使用低级库,但我可能找到了适合您的情况的代码:

    private int IsMuted() {
        IAudioEndpointVolume masterVol = null;

        try {
            masterVol = GetMasterVolumeObject();
            if( masterVol == null )
                return -1; //error

            bool isMuted;
            masterVol.GetMute( out isMuted );

            return Convert.ToInt32( isMuted );
        }
        finally {
            if( masterVol != null )
                Marshal.ReleaseComObject( masterVol );
        }
    }

    private IAudioEndpointVolume GetMasterVolumeObject() {
        IMMDeviceEnumerator deviceEnumerator = null;
        IMMDevice speakers = null;
        try {
            deviceEnumerator = (IMMDeviceEnumerator)( new MMDeviceEnumerator() );
            deviceEnumerator.GetDefaultAudioEndpoint( EDataFlow.eRender, ERole.eMultimedia, out speakers );

            Guid IID_IAudioEndpointVolume = typeof( IAudioEndpointVolume ).GUID;
            object o;
            speakers.Activate( ref IID_IAudioEndpointVolume, 0, IntPtr.Zero, out o );
            IAudioEndpointVolume masterVol = (IAudioEndpointVolume)o;

            return masterVol;
        }
        finally {
            if( speakers != null ) Marshal.ReleaseComObject( speakers );
            if( deviceEnumerator != null ) Marshal.ReleaseComObject( deviceEnumerator );
        }
    }

来自 Windows CoreAudio API 的 COM 接口

    [ComImport]
    [Guid( "BCDE0395-E52F-467C-8E3D-C4579291692E" )]
    internal class MMDeviceEnumerator {
    }

    internal enum EDataFlow {
        eRender,
        eCapture,
        eAll,
        EDataFlow_enum_count
    }

    internal enum ERole {
        eConsole,
        eMultimedia,
        eCommunications,
        ERole_enum_count
    }

    [Guid( "A95664D2-9614-4F35-A746-DE8DB63617E6" ), InterfaceType( ComInterfaceType.InterfaceIsIUnknown )]
    internal interface IMMDeviceEnumerator {
        int NotImpl1();

        [PreserveSig]
        int GetDefaultAudioEndpoint( EDataFlow dataFlow, ERole role, out IMMDevice ppDevice );

        // the rest is not implemented
    }

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

        // the rest is not implemented
    }

    // http://netcoreaudio.codeplex.com/SourceControl/latest#trunk/Code/CoreAudio/Interfaces/IAudioEndpointVolume.cs
    [Guid( "5CDF2C82-841E-4546-9722-0CF74078229A" ), InterfaceType( ComInterfaceType.InterfaceIsIUnknown )]
    public interface IAudioEndpointVolume {
        [PreserveSig]
        int NotImpl1();

        [PreserveSig]
        int NotImpl2();

        /// <summary>
        /// Gets a count of the channels in the audio stream.
        /// </summary>
        /// <param name="channelCount">The number of channels.</param>
        /// <returns>An HRESULT code indicating whether the operation passed of failed.</returns>
        [PreserveSig]
        int GetChannelCount(
            [Out] [MarshalAs( UnmanagedType.U4 )] out UInt32 channelCount );

        /// <summary>
        /// Sets the master volume level of the audio stream, in decibels.
        /// </summary>
        /// <param name="level">The new master volume level in decibels.</param>
        /// <param name="eventContext">A user context value that is passed to the notification callback.</param>
        /// <returns>An HRESULT code indicating whether the operation passed of failed.</returns>
        [PreserveSig]
        int SetMasterVolumeLevel(
            [In] [MarshalAs( UnmanagedType.R4 )] float level,
            [In] [MarshalAs( UnmanagedType.LPStruct )] Guid eventContext );

        /// <summary>
        /// Sets the master volume level, expressed as a normalized, audio-tapered value.
        /// </summary>
        /// <param name="level">The new master volume level expressed as a normalized value between 0.0 and 1.0.</param>
        /// <param name="eventContext">A user context value that is passed to the notification callback.</param>
        /// <returns>An HRESULT code indicating whether the operation passed of failed.</returns>
        [PreserveSig]
        int SetMasterVolumeLevelScalar(
            [In] [MarshalAs( UnmanagedType.R4 )] float level,
            [In] [MarshalAs( UnmanagedType.LPStruct )] Guid eventContext );

        /// <summary>
        /// Gets the master volume level of the audio stream, in decibels.
        /// </summary>
        /// <param name="level">The volume level in decibels.</param>
        /// <returns>An HRESULT code indicating whether the operation passed of failed.</returns>
        [PreserveSig]
        int GetMasterVolumeLevel(
            [Out] [MarshalAs( UnmanagedType.R4 )] out float level );

        /// <summary>
        /// Gets the master volume level, expressed as a normalized, audio-tapered value.
        /// </summary>
        /// <param name="level">The volume level expressed as a normalized value between 0.0 and 1.0.</param>
        /// <returns>An HRESULT code indicating whether the operation passed of failed.</returns>
        [PreserveSig]
        int GetMasterVolumeLevelScalar(
            [Out] [MarshalAs( UnmanagedType.R4 )] out float level );

        /// <summary>
        /// Sets the volume level, in decibels, of the specified channel of the audio stream.
        /// </summary>
        /// <param name="channelNumber">The channel number.</param>
        /// <param name="level">The new volume level in decibels.</param>
        /// <param name="eventContext">A user context value that is passed to the notification callback.</param>
        /// <returns>An HRESULT code indicating whether the operation passed of failed.</returns>
        [PreserveSig]
        int SetChannelVolumeLevel(
            [In] [MarshalAs( UnmanagedType.U4 )] UInt32 channelNumber,
            [In] [MarshalAs( UnmanagedType.R4 )] float level,
            [In] [MarshalAs( UnmanagedType.LPStruct )] Guid eventContext );

        /// <summary>
        /// Sets the normalized, audio-tapered volume level of the specified channel in the audio stream.
        /// </summary>
        /// <param name="channelNumber">The channel number.</param>
        /// <param name="level">The new master volume level expressed as a normalized value between 0.0 and 1.0.</param>
        /// <param name="eventContext">A user context value that is passed to the notification callback.</param>
        /// <returns>An HRESULT code indicating whether the operation passed of failed.</returns>
        [PreserveSig]
        int SetChannelVolumeLevelScalar(
            [In] [MarshalAs( UnmanagedType.U4 )] UInt32 channelNumber,
            [In] [MarshalAs( UnmanagedType.R4 )] float level,
            [In] [MarshalAs( UnmanagedType.LPStruct )] Guid eventContext );

        /// <summary>
        /// Gets the volume level, in decibels, of the specified channel in the audio stream.
        /// </summary>
        /// <param name="channelNumber">The zero-based channel number.</param>
        /// <param name="level">The volume level in decibels.</param>
        /// <returns>An HRESULT code indicating whether the operation passed of failed.</returns>
        [PreserveSig]
        int GetChannelVolumeLevel(
            [In] [MarshalAs( UnmanagedType.U4 )] UInt32 channelNumber,
            [Out] [MarshalAs( UnmanagedType.R4 )] out float level );

        /// <summary>
        /// Gets the normalized, audio-tapered volume level of the specified channel of the audio stream.
        /// </summary>
        /// <param name="channelNumber">The zero-based channel number.</param>
        /// <param name="level">The volume level expressed as a normalized value between 0.0 and 1.0.</param>
        /// <returns>An HRESULT code indicating whether the operation passed of failed.</returns>
        [PreserveSig]
        int GetChannelVolumeLevelScalar(
            [In] [MarshalAs( UnmanagedType.U4 )] UInt32 channelNumber,
            [Out] [MarshalAs( UnmanagedType.R4 )] out float level );

        /// <summary>
        /// Sets the muting state of the audio stream.
        /// </summary>
        /// <param name="isMuted">True to mute the stream, or false to unmute the stream.</param>
        /// <param name="eventContext">A user context value that is passed to the notification callback.</param>
        /// <returns>An HRESULT code indicating whether the operation passed of failed.</returns>
        [PreserveSig]
        int SetMute(
            [In] [MarshalAs( UnmanagedType.Bool )] Boolean isMuted,
            [In] [MarshalAs( UnmanagedType.LPStruct )] Guid eventContext );

        /// <summary>
        /// Gets the muting state of the audio stream.
        /// </summary>
        /// <param name="isMuted">The muting state. True if the stream is muted, false otherwise.</param>
        /// <returns>An HRESULT code indicating whether the operation passed of failed.</returns>
        [PreserveSig]
        int GetMute(
            [Out] [MarshalAs( UnmanagedType.Bool )] out Boolean isMuted );

        /// <summary>
        /// Gets information about the current step in the volume range.
        /// </summary>
        /// <param name="step">The current zero-based step index.</param>
        /// <param name="stepCount">The total number of steps in the volume range.</param>
        /// <returns>An HRESULT code indicating whether the operation passed of failed.</returns>
        [PreserveSig]
        int GetVolumeStepInfo(
            [Out] [MarshalAs( UnmanagedType.U4 )] out UInt32 step,
            [Out] [MarshalAs( UnmanagedType.U4 )] out UInt32 stepCount );

        /// <summary>
        /// Increases the volume level by one step.
        /// </summary>
        /// <param name="eventContext">A user context value that is passed to the notification callback.</param>
        /// <returns>An HRESULT code indicating whether the operation passed of failed.</returns>
        [PreserveSig]
        int VolumeStepUp(
            [In] [MarshalAs( UnmanagedType.LPStruct )] Guid eventContext );

        /// <summary>
        /// Decreases the volume level by one step.
        /// </summary>
        /// <param name="eventContext">A user context value that is passed to the notification callback.</param>
        /// <returns>An HRESULT code indicating whether the operation passed of failed.</returns>
        [PreserveSig]
        int VolumeStepDown(
            [In] [MarshalAs( UnmanagedType.LPStruct )] Guid eventContext );

        /// <summary>
        /// Queries the audio endpoint device for its hardware-supported functions.
        /// </summary>
        /// <param name="hardwareSupportMask">A hardware support mask that indicates the capabilities of the endpoint.</param>
        /// <returns>An HRESULT code indicating whether the operation passed of failed.</returns>
        [PreserveSig]
        int QueryHardwareSupport(
            [Out] [MarshalAs( UnmanagedType.U4 )] out UInt32 hardwareSupportMask );

        /// <summary>
        /// Gets the volume range of the audio stream, in decibels.
        /// </summary>
        /// <param name="volumeMin">The minimum volume level in decibels.</param>
        /// <param name="volumeMax">The maximum volume level in decibels.</param>
        /// <param name="volumeStep">The volume increment level in decibels.</param>
        /// <returns>An HRESULT code indicating whether the operation passed of failed.</returns>
        [PreserveSig]
        int GetVolumeRange(
            [Out] [MarshalAs( UnmanagedType.R4 )] out float volumeMin,
            [Out] [MarshalAs( UnmanagedType.R4 )] out float volumeMax,
            [Out] [MarshalAs( UnmanagedType.R4 )] out float volumeStep );
    }

音频管理器.cs

© www.soinside.com 2019 - 2024. All rights reserved.