簡體   English   中英

在C#中將主音頻音量從XP更改為Windows 8

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

我需要一些通用方法將主音頻音量從Windows XP更改為C#中的Windows 8,因為我的應用程序將在這些OS上運行。

我已經嘗試過http://www.geekpedia.com/tutorial176_Get-and-set-the-wave-sound-volume.html,但是在Windows 8下不起作用。也許在Windows XP下可以工作。

無論如何,我需要一些兼容的方法來做到這一點。 有什么線索嗎?

所以我的解決方案是結合兩個項目:

  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

對於Windows 7+:

接受的答案存在一些問題。 由於codeproject頁面已刪除,因此它現在沒有上下文。

  1. 您需要從Nuget獲取NAudio

  2. 用第二個替換第一個

    MMDevice設備= DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender,ERole.eMultimedia);

    MMDevice設備= DevEnum.GetDefaultAudioEndpoint((DataFlow)0,(角色)1);

如果您在嘗試使用可接受的答案代碼修復錯誤時迷失了方向,請快速告知。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM