【Roger Wo】在.NET程式中控制系統音量
在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
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部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-349324/,如需轉載,請註明出處,否則將追究法律責任。
請登入後發表評論
登入
全部評論
相關文章
- iOS 調節系統音量iOS
- Platform SDK 中控制系統音量的重要結構MIXERCONTROLDETAILS結構體定義 (轉)PlatformAI結構體
- windows10系統下音量控制不見了如何解決Windows
- windows10系統中增加麥克風音量的方法Windows
- 在MacOS系統中如何管理隱私許可權控制?Mac
- 在Windows10系統中同步Internet 時間Windows
- Kubernetes在華為全球 IT系統中的實踐
- Platform SDK 中控制系統音量的另一個重要結構MIXERLINECONTROLS結構體定義 (轉)Platform結構體
- android軟體音量控制Android
- 一個利用windows api控制放音音量和錄音麥克風音量實現類(c++程式碼)WindowsAPIC++
- Docker容器中執行.net framework控制檯程式DockerFramework
- 在.NET中輕鬆獲取系統資訊(1) -WMI篇 (轉)
- Insider使用者可使用!微軟釋出新win11版本,系統音量圖示可控制應用程式靜音IDE微軟
- 作業系統實驗——程式控制作業系統
- Win10系統怎麼找回win7舊版音量控制器【圖文】Win10Win7
- 作業系統實驗2 程式控制和系統呼叫作業系統
- 在VB.Net中建立使用控制元件陣列 (轉)控制元件陣列
- Windows10系統怎麼設定音量快捷鍵Windows
- 低程式碼在ERP系統實施中的作用
- .NET App 與Windows系統媒體控制(SMTC)互動APPWindows
- 在 Ali Kubernetes 系統中,我們這樣實踐混沌工程
- Opencv專案實戰:14 手勢控制音量OpenCV
- 轉貼_roger大師_
- 怎樣理解控制系統中的頻寬
- macOS系統中的家長控制管理教程Mac
- Azure Data Lake(一) 在NET Core 控制檯中操作 Data Lake Storage
- asp.net 在使用母版頁的子頁面cs後臺程式碼中控制母版頁中的登入控制元件顯示ASP.NET控制元件
- 如何減小Win8系統其它聲音的音量
- LINUX系統中程式如何管理控制(一)Linux
- 作業系統3——程式的描述與控制作業系統
- 如何讓任何小程式都支援在windows系統中開啟?Windows
- 在Delphi程式設計中獲取作業系統資訊 (轉)程式設計作業系統
- 版本控制系統
- 不一樣的控制檯程式—–在控制檯中輸出圖片薦
- 微軟第三方Win10音量控制應用程式EarTrumpet微軟Win10
- win7音訊 audio service調節單個程式音量控制Win7音訊
- 在 Python 程式中啟動 mitmproxy 並控制 addonPythonMIT
- 在Delphi中捕獲控制檯程式的輸出 (轉)