本文介绍了从XP在C#中更改主音量到Windows 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些通用的方法,从Windows XP中更改主音量到Windows 8在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.

我已经 HTTP尝试:// WWW。 geekpedia.com/tutorial176_Get-and-set-the-wave-sound-volume.html 但它无法在Windows 8下工作也许还应该在Windows XP下正常工作。

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.

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

Anyway I need some compatible approach to do it. Any clue?

推荐答案

所以我的解决方案是结合2个项目:

So my solutions is to combine 2 projects:

  1. Mute/unmute,用C#在Windows 7 64位系统更改主音量

  1. Mute/unmute, Change master volume in Windows 7 x64 with C#

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

最后code应该是这样

The final code should be like

    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;
        }
    }

这篇关于从XP在C#中更改主音量到Windows 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 00:31