在windows下控制系统音量,需要通过使用win32的WDM audio components(winmm.dll)来实现,为了方便起见,将其封装到了一个AudioMixerHelper类中,可以直接通过GetVolume()和SetVolume方法来改变音量。
using System;
using System.Runtime.InteropServices;
namespace Microsoft


{
public class AudioMixerHelper

{
public const int MMSYSERR_NOERROR = 0;
public const int MAXPNAMELEN = 32;
public const int MIXER_LONG_NAME_CHARS = 64;
public const int MIXER_SHORT_NAME_CHARS = 16;
public const int MIXER_GETLINEINFOF_COMPONENTTYPE = 0x3;
public const int MIXER_GETCONTROLDETAILSF_VALUE = 0x0;
public const int MIXER_GETLINECONTROLSF_ONEBYTYPE = 0x2;
public const int MIXER_SETCONTROLDETAILSF_VALUE = 0x0;
public const int MIXERLINE_COMPONENTTYPE_DST_FIRST = 0x0;
public const int MIXERLINE_COMPONENTTYPE_SRC_FIRST = 0x1000;
public const int MIXERLINE_COMPONENTTYPE_DST_SPEAKERS =
(MIXERLINE_COMPONENTTYPE_DST_FIRST + 4);
public const int MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE =
(MIXERLINE_COMPONENTTYPE_SRC_FIRST + 3);
public const int MIXERLINE_COMPONENTTYPE_SRC_LINE =
(MIXERLINE_COMPONENTTYPE_SRC_FIRST + 2);
public const int MIXERCONTROL_CT_CLASS_FADER = 0x50000000;
public const int MIXERCONTROL_CT_UNITS_UNSIGNED = 0x30000;
public const int MIXERCONTROL_CONTROLTYPE_FADER =
(MIXERCONTROL_CT_CLASS_FADER | MIXERCONTROL_CT_UNITS_UNSIGNED);
public const int MIXERCONTROL_CONTROLTYPE_VOLUME =
(MIXERCONTROL_CONTROLTYPE_FADER + 1);
[DllImport("winmm.dll", CharSet=CharSet.Ansi)]
private static extern int mixerClose (int hmx);
[DllImport("winmm.dll", CharSet=CharSet.Ansi)]
private static extern int mixerGetControlDetailsA (int hmxobj,ref
MIXERCONTROLDETAILS pmxcd , int fdwDetails);
[DllImport("winmm.dll", CharSet=CharSet.Ansi)]
private static extern int mixerGetDevCapsA(int uMxId, MIXERCAPS
pmxcaps, int cbmxcaps);
[DllImport("winmm.dll", CharSet=CharSet.Ansi)]
private static extern int mixerGetID (int hmxobj, int pumxID, int
fdwId);
[DllImport("winmm.dll", CharSet=CharSet.Ansi)]
private static extern int mixerGetLineControlsA (int hmxobj,ref
MIXERLINECONTROLS pmxlc, int fdwControls);
[DllImport("winmm.dll", CharSet=CharSet.Ansi)]
private static extern int mixerGetLineInfoA (int hmxobj,ref
MIXERLINE pmxl , int fdwInfo);
[DllImport("winmm.dll", CharSet=CharSet.Ansi)]
private static extern int mixerGetNumDevs();
[DllImport("winmm.dll", CharSet=CharSet.Ansi)]
private static extern int mixerMessage(int hmx , int uMsg , int
dwParam1 , int dwParam2);
[DllImport("winmm.dll", CharSet=CharSet.Ansi)]
private static extern int mixerOpen (out int phmx , int uMxId ,
int dwCallback , int dwInstance , int fdwOpen);
[DllImport("winmm.dll", CharSet=CharSet.Ansi)]
private static extern int mixerSetControlDetails(int hmxobj ,ref
MIXERCONTROLDETAILS pmxcd , int fdwDetails);
public struct MIXERCAPS

{
public int wMid;
public int wPid;
public int vDriverVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=MAXPNAMELEN)]
public string szPname;
public int fdwSupport;
public int cDestinations;
}
public struct MIXERCONTROL

{
public int cbStruct;
public int dwControlID;
public int dwControlType;
public int fdwControl;
public int cMultipleItems;
[MarshalAs( UnmanagedType.ByValTStr,
SizeConst=MIXER_SHORT_NAME_CHARS)]
public string szShortName ;
[MarshalAs( UnmanagedType.ByValTStr,
SizeConst=MIXER_LONG_NAME_CHARS)]
public string szName;
public int lMinimum;
public int lMaximum;
[MarshalAs(UnmanagedType.U4, SizeConst=10)]
public int reserved;
}
public struct MIXERCONTROLDETAILS

{
public int cbStruct;
public int dwControlID;
public int cChannels;
public int item;
public int cbDetails;
public IntPtr paDetails;
}
public struct MIXERCONTROLDETAILS_UNSIGNED

{
public int dwValue;
}
public struct MIXERLINE

{
public int cbStruct;
public int dwDestination;
public int dwSource;
public int dwLineID;
public int fdwLine;
public int dwUser;
public int dwComponentType;
public int cChannels;
public int cConnections;
public int cControls;
[MarshalAs(UnmanagedType.ByValTStr,
&nbs
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-349324/
,如需转载,请注明出处,否则将追究法律责任。