简体   繁体   English

在C#中将主音频音量从XP更改为Windows 8

[英]Change master audio volume from XP to Windows 8 in C#

I need some general method to change master audio volume from Windows XP to Windows 8 in C# because my application is going to work on those OS. 我需要一些通用方法将主音频音量从Windows XP更改为C#中的Windows 8,因为我的应用程序将在这些OS上运行。

I have tried already http://www.geekpedia.com/tutorial176_Get-and-set-the-wave-sound-volume.html but it doesn't work under Windows 8. Perhaps it should work under Windows XP. 我已经尝试过http://www.geekpedia.com/tutorial176_Get-and-set-the-wave-sound-volume.html,但是在Windows 8下不起作用。也许在Windows XP下可以工作。

Anyway I need some compatible approach to do it. 无论如何,我需要一些兼容的方法来做到这一点。 Any clue? 有什么线索吗?

So my solutions is to combine 2 projects: 所以我的解决方案是结合两个项目:

  1. Mute/unmute, Change master volume in Windows 7 x64 with C# 静音/取消静音,使用C#在Windows 7 x64中更改主音量

  2. http://www.geekpedia.com/tutorial176_Get-and-set-the-wave-sound-volume.html http://www.geekpedia.com/tutorial176_Get-and-set-the-wave-sound-volume.html

    The final code should be like (It uses NAudio framework) 最终代码应类似于(它使用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; } } 

Update #1. 更新#1。 Year 2015 Basically it uses NAudio framework. 2015年基本上,它使用NAudio框架。 So nowdays some methods and properties of NAudio have other names. 因此,如今NAudio的某些方法和属性使用其他名称。

For instance 例如

eDataFlow.eRender is now DataFlow.Render eDataFlow.eRender现在是DataFlow.Render

and

eRole.eMultimedia is Role.Multimedia eRole.eMultimedia是Role.Multimedia

For windows 7+: 对于Windows 7+:

There are some problems with the accepted answer. 接受的答案存在一些问题。 Because the codeproject page is deleted it now has no context. 由于codeproject页面已删除,因此它现在没有上下文。

  1. You need to get NAudio from Nuget 您需要从Nuget获取NAudio

  2. Replace the first with the second 用第二个替换第一个

    MMDevice device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia); MMDevice设备= DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender,ERole.eMultimedia);

    MMDevice device = DevEnum.GetDefaultAudioEndpoint((DataFlow)0, (Role)1); MMDevice设备= DevEnum.GetDefaultAudioEndpoint((DataFlow)0,(角色)1);

Just a quick heads-up if you are lost trying to fix the errors with the accepted-answer code. 如果您在尝试使用可接受的答案代码修复错误时迷失了方向,请快速告知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM