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

标签 c# .net audio

我需要一些通用的方法来将主音频音量从Windows XP更改为C#中的Windows 8,因为我的应用程序将在这些OS上运行。

我已经尝试过http://www.geekpedia.com/tutorial176_Get-and-set-the-wave-sound-volume.html,但是在Windows 8下不起作用。也许它应该在Windows XP下工作。

无论如何,我需要一些兼容的方法来做到这一点。有什么线索吗?

最佳答案

所以我的解决方案是合并两个项目:

  • Mute/unmute, Change master volume in Windows 7 x64 with C#
  • 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 is now DataFlow.Render





    eRole.eMultimedia is Role.Multimedia

    关于c# - 在C#中将主音频音量从XP更改为Windows 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15250379/

    相关文章:

    c# - 我应该使用结构还是类来表示 Lat/Lng 坐标?

    c# - 在 MVC cshtml 中每行显示三列

    c# - 在 BackgroundWorker 线程上创建 FlowDocument

    c# - 向datagridview c#添加新的空白行

    c# - 具有固定平均值的随机数

    swift - channel AVAudioRecorder Swift 3

    c# - 我的类应该实现什么接口(interface)或方法来在 Console.WriteLine 中打印我想要的内容?

    c# - 我可以对事件目录使用基于 token 的身份验证吗?

    android - 尝试使用 Activity 的 EditText 中的字符串作为记录 Activity 的文件名

    vb.net - 在 vb 中播放音频时出现 AccessViolationException