0

私のアプリケーションはこれらの OS で動作するため、マスター オーディオ ボリュームを C# で Windows XP から Windows 8 に変更する一般的な方法が必要です。

私はすでにhttp://www.geekpedia.com/tutorial176_Get-and-set-the-wave-sound-volume.htmlを試し ましたが、Windows 8 では動作しません。おそらく Windows XP では動作するはずです。

とにかく、それを行うには互換性のあるアプローチが必要です。どんな手掛かり?

4

2 に答える 2

5

だから私の解決策は2つのプロジェクトを結合することです:

  1. C#を使用したWindows 7 x64でのミュート/ミュート解除、マスターボリュームの変更

  2. 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 です

于 2013-03-06T15:41:49.323 に答える
1

Windows 7 以降の場合:

受け入れられた答えにはいくつかの問題があります。codeproject ページが削除されたため、現在はコンテキストがありません。

  1. Nuget から NAudio を取得する必要があります

  2. 1 番目を 2 番目に置き換えます

    MMDevice デバイス = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);

    MMDevice device = DevEnum.GetDefaultAudioEndpoint((DataFlow)0, (Role)1);

受け入れられた回答コードでエラーを修正しようとして迷った場合は、簡単に注意してください。

于 2015-01-07T11:28:34.940 に答える