我需要一些通用方法在 C# 中将主音量从 Windows XP 更改为 Windows 8,因为我的应用程序将在这些操作系统上运行。
我已经尝试过http://www.geekpedia.com/tutorial176_Get-and-set-the-wave-sound-volume.html 但它在 Windows 8 下不起作用。也许它应该在 Windows XP 下工作。
无论如何,我需要一些兼容的方法来做到这一点。有什么线索吗?
我需要一些通用方法在 C# 中将主音量从 Windows XP 更改为 Windows 8,因为我的应用程序将在这些操作系统上运行。
我已经尝试过http://www.geekpedia.com/tutorial176_Get-and-set-the-wave-sound-volume.html 但它在 Windows 8 下不起作用。也许它应该在 Windows XP 下工作。
无论如何,我需要一些兼容的方法来做到这一点。有什么线索吗?
所以我的解决方案是结合两个项目:
http://www.geekpedia.com/tutorial176_Get-and-set-the-wave-sound-volume.html
最终代码应该是这样的(它使用 NAudio 框架)
static class NativeMethods
{
[DllImport("winmm.dll", EntryPoint = "waveOutSetVolume")]
public static extern int WaveOutSetVolume(IntPtr hwo, uint dwVolume);
[DllImport("winmm.dll", SetLastError = true)]
public static extern bool PlaySound(string pszSound, IntPtr hmod, uint fdwSound);
}
public static class MSWindowsFriendlyNames
{
public static string WindowsXP { get { return "Windows XP"; } }
public static string WindowsVista { get { return "Windows Vista"; } }
public static string Windows7 { get { return "Windows 7"; } }
public static string Windows8 { get { return "Windows 8"; } }
}
public static class SistemVolumChanger
{
public static void SetVolume(int value)
{
if (value < 0)
value = 0;
if (value > 100)
value = 100;
var osFriendlyName = GetOSFriendlyName();
if (osFriendlyName.Contains(MSWindowsFriendlyNames.WindowsXP))
{
SetVolumeForWIndowsXP(value);
}
else if (osFriendlyName.Contains(MSWindowsFriendlyNames.WindowsVista) || osFriendlyName.Contains(MSWindowsFriendlyNames.Windows7) || osFriendlyName.Contains(MSWindowsFriendlyNames.Windows8))
{
SetVolumeForWIndowsVista78(value);
}
else
{
SetVolumeForWIndowsVista78(value);
}
}
public static int GetVolume()
{
int result = 100;
try
{
MMDeviceEnumerator DevEnum = new MMDeviceEnumerator();
MMDevice device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
result = (int)(device.AudioEndpointVolume.MasterVolumeLevelScalar * 100);
}
catch (Exception)
{
}
return result;
}
private static void SetVolumeForWIndowsVista78(int value)
{
try
{
MMDeviceEnumerator DevEnum = new MMDeviceEnumerator();
MMDevice device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
device.AudioEndpointVolume.MasterVolumeLevelScalar = (float)value / 100.0f;
}
catch (Exception)
{
}
}
private static void SetVolumeForWIndowsXP(int value)
{
try
{
// Calculate the volume that's being set
double newVolume = ushort.MaxValue * value / 10.0;
uint v = ((uint)newVolume) & 0xffff;
uint vAll = v | (v << 16);
// Set the volume
int retVal = NativeMethods.WaveOutSetVolume(IntPtr.Zero, vAll);
}
catch (Exception)
{
}
}
private static string GetOSFriendlyName()
{
string result = string.Empty;
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem");
foreach (ManagementObject os in searcher.Get())
{
result = os["Caption"].ToString();
break;
}
return result;
}
}
更新#1。2015 年 基本上它使用 NAudio 框架。所以现在 NAudio的一些方法和属性有其他的名字。
例如
eDataFlow.eRender 现在是 DataFlow.Render
和
eRole.eMultimedia 是 Role.Multimedia
对于 Windows 7+:
接受的答案存在一些问题。因为 codeproject 页面已被删除,所以它现在没有上下文。
您需要从 Nuget 获取 NAudio
用第二个代替第一个
MMDevice 设备 = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
MMDevice device = DevEnum.GetDefaultAudioEndpoint((DataFlow)0, (Role)1);
如果您在尝试使用接受的答案代码修复错误时迷失了方向,请快速提醒一下。