[英]How to access current system audio sound to see actual sound volume?
你可以试试这个链接:
http://www.dreamincode.net/forums/topic/45693-controlling-sound-volume-in-c%23/
基本上,您需要导入这个 win32 api 函数:
[DllImport("winmm.dll")]
public static extern int waveOutGetVolume(IntPtr hwo, out uint dwVolume);
那么你可以这样使用它:
waveOutGetVolume(IntPtr.Zero, out CurrVol);
您将获得“CurrVol”的音量级别
下载类 NAudio 并引用 C# 项目中的 DLL。
然后将以下代码添加到您的项目中。 它将枚举所有音频设备获得其音量并尝试将其静音。
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
{
//Get its audio volume
System.Diagnostics.Debug.Print("Volume of " + dev.FriendlyName + " is " + dev.AudioEndpointVolume.MasterVolumeLevel.ToString());
//Mute it
dev.AudioEndpointVolume.Mute = true;
System.Diagnostics.Debug.Print(dev.FriendlyName + " is muted");
//Get its audio volume
System.Diagnostics.Debug.Print(dev.AudioEndpointVolume.MasterVolumeLevel.ToString());
}
catch (Exception ex)
{
//Do something with exception when an audio endpoint could not be muted
System.Diagnostics.Debug.Print(dev.FriendlyName + " could not be muted");
}
}
}
catch (Exception ex)
{
//When something happend that prevent us to iterate through the devices
System.Diagnostics.Debug.Print("Could not enumerate devices due to an excepion: " + ex.Message);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.