diff --git a/mediaportal/Core/Mixer/Mixer10.cs b/mediaportal/Core/Mixer/Mixer10.cs index ba43bf86896..f7c9054b3f7 100644 --- a/mediaportal/Core/Mixer/Mixer10.cs +++ b/mediaportal/Core/Mixer/Mixer10.cs @@ -289,6 +289,10 @@ public void Open(int mixerIndex, bool isDigital, int[] volumeTable) if (_mMdeviceEnumerator == null) _mMdeviceEnumerator = new MMDeviceEnumerator(); + //Init Mute state + if (iAudioEndpointVolume != null) + _isMuted = iAudioEndpointVolume.IsMuted; + var mMdeviceList = _mMdeviceEnumerator.EnumAudioEndpoints(DataFlow.Render, DeviceState.Active); if (mMdeviceList != null && mMdeviceList.Count > 0) diff --git a/mediaportal/Core/Player/VolumeHandler.cs b/mediaportal/Core/Player/VolumeHandler.cs index fab8691f355..84b20c5a5b7 100644 --- a/mediaportal/Core/Player/VolumeHandler.cs +++ b/mediaportal/Core/Player/VolumeHandler.cs @@ -48,9 +48,20 @@ public class VolumeHandler #region Constructors - public VolumeHandler() : this(LoadFromRegistry()) { } + protected VolumeHandler() + { + } public VolumeHandler(int[] volumeTable) + { + this.Init(volumeTable); + } + + #endregion Constructors + + #region Methods + + protected void Init(int[] volumeTable) { if (OSInfo.OSInfo.Win10OrLater()) { @@ -98,7 +109,7 @@ public VolumeHandler(int[] volumeTable) } catch (Exception ex) { - Log.Error("VolumeHandler: Mixer exception during init {0}", ex); + Log.Error("VolumeHandler: Init() Mixer exception during init {0}", ex); } if (OSInfo.OSInfo.Win8OrLater() && hideWindowsOSD) @@ -165,7 +176,7 @@ public VolumeHandler(int[] volumeTable) } catch (Exception ex) { - Log.Error("VolumeHandler: Mixer exception when init {0}", ex); + Log.Error("VolumeHandler: Init() Mixer exception when init {0}", ex); } if (OSInfo.OSInfo.Win8OrLater() && hideWindowsOSD) @@ -196,11 +207,7 @@ public VolumeHandler(int[] volumeTable) } public bool hideWindowsOSD { get; set; } - - #endregion Constructors - - #region Methods - + /// /// Create our volume handler singleton. /// @@ -243,7 +250,7 @@ private static VolumeHandler Create() {0, 6553, 13106, 19659, 26212, 32765, 39318, 45871, 52424, 58977, 65535}); // windows default from registry case 1: - return new VolumeHandler(); + return new VolumeHandler(LoadFromRegistry()); // logarithmic case 2: return new VolumeHandler(new[] @@ -296,7 +303,7 @@ private static VolumeHandler Create() {0, 6553, 13106, 19659, 26212, 32765, 39318, 45871, 52424, 58977, 65535}); // windows default from registry case 1: - return new VolumeHandler(); + return new VolumeHandler(LoadFromRegistry()); // logarithmic case 2: return new VolumeHandler(new[] @@ -393,7 +400,7 @@ public virtual void UnMute() } } - private static int[] LoadFromRegistry() + protected static int[] LoadFromRegistry() { using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Multimedia\Audio\VolumeControl")) { diff --git a/mediaportal/Core/Player/VolumeHandlerCustom.cs b/mediaportal/Core/Player/VolumeHandlerCustom.cs index 88cb769c8ea..16ace9708d4 100644 --- a/mediaportal/Core/Player/VolumeHandlerCustom.cs +++ b/mediaportal/Core/Player/VolumeHandlerCustom.cs @@ -22,6 +22,7 @@ using System.Collections; using MediaPortal.Configuration; using MediaPortal.Profile; +using MediaPortal.GUI.Library; namespace MediaPortal.Player { @@ -36,34 +37,39 @@ public VolumeHandlerCustom() string text = reader.GetValueAsString("volume", "table", "0, 4095, 8191, 1638, 12287, 16383, 20479, 24575, 28671, 32767, 36863, 40959, 45055, 49151, 53247, 57343, 61439, 65535"); - if (text == string.Empty) + if (!string.IsNullOrWhiteSpace(text)) { - return; - } - - ArrayList array = new ArrayList(); + ArrayList array = new ArrayList(); - try - { - foreach (string volume in text.Split(new char[] {',', ';'})) + try { - if (volume == string.Empty) + foreach (string volume in text.Split(new char[] {',', ';'})) { - continue; + if (volume == string.Empty) + { + continue; + } + + array.Add(Math.Max(this.Minimum, Math.Min(this.Maximum, int.Parse(volume)))); } - array.Add(Math.Max(this.Minimum, Math.Min(this.Maximum, int.Parse(volume)))); - } + array.Sort(); - array.Sort(); + this.Init((int[])array.ToArray(typeof(int))); - this.Table = (int[])array.ToArray(typeof (int)); - } - catch - { - // heh, its undocumented remember, no fancy logging going on here + Log.Debug("VolumeHandlerCustom: ctor() table loaded: {0}", text); + + return; + } + catch (Exception ex) + { + Log.Error("VolumeHandlerCustom: ctor() {0}", ex.Message); + } } } + + //Default + this.Init(LoadFromRegistry()); } #endregion Constructors diff --git a/mediaportal/Core/Util/HideVolumeOSDLib.cs b/mediaportal/Core/Util/HideVolumeOSDLib.cs index 07326dc2aed..f21eedff2f6 100644 --- a/mediaportal/Core/Util/HideVolumeOSDLib.cs +++ b/mediaportal/Core/Util/HideVolumeOSDLib.cs @@ -28,114 +28,141 @@ namespace HideVolumeOSD { - public class HideVolumeOSDLib - { - [DllImport("user32.dll")] - static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo); - - [DllImport("user32.dll", SetLastError = true)] - static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); - - [DllImport("user32.dll", SetLastError = true)] - static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); - - IntPtr hWndInject = IntPtr.Zero; - - public HideVolumeOSDLib(bool IsMuted) - { - hWndInject = FindOSDWindow(); - - int count = 0; - - while (hWndInject == IntPtr.Zero && count < 10) - { - if (IsMuted) - { - keybd_event((byte)Keys.VolumeMute, 0, 0, 0); - keybd_event((byte)Keys.VolumeMute, 0, 0, 0); - } - else - { - keybd_event((byte)Keys.VolumeUp, 0, 0, 0); - keybd_event((byte)Keys.VolumeDown, 0, 0, 0); - } - - System.Threading.Thread.Sleep(500); - - hWndInject = FindOSDWindow(); - - count++; - } - - if (hWndInject == IntPtr.Zero) - { - Log.Error("HideVolumeOSD: VolumeOSD not found."); - } - } - - private IntPtr FindOSDWindow() - { - IntPtr hwndRet = IntPtr.Zero; - IntPtr hwndHost = IntPtr.Zero; - - // search for window with class 'NativeHWNDHost' - - int pairCount = 0; - - string strClass, strClassInner, strNameInner; - - if (Environment.OSVersion.Version.Build >= 22000) - { - //Windows 11 - strClass = "XamlExplorerHostIslandWindow"; - strClassInner = "Windows.UI.Composition.DesktopWindowContentBridge"; - strNameInner = "DesktopWindowXamlSource"; - } - else - { - strClass = "NativeHWNDHost"; - strClassInner = "DirectUIHWND"; - strNameInner = string.Empty; - } - - while ((hwndHost = FindWindowEx(IntPtr.Zero, hwndHost, strClass, "")) != IntPtr.Zero) - { - // if this window has a child with class 'DirectUIHWND' it should be the volume OSD - - if (FindWindowEx(hwndHost, IntPtr.Zero, strClassInner, strNameInner) != IntPtr.Zero) + public class HideVolumeOSDLib + { + private const int KEYEVENTF_KEYUP = 2; + + [DllImport("user32.dll")] + static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo); + + [DllImport("user32.dll", SetLastError = true)] + static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); + + [DllImport("user32.dll", SetLastError = true)] + static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); + + IntPtr hWndInject = IntPtr.Zero; + + public HideVolumeOSDLib(bool IsMuted) { - if (pairCount == 0) - { - hwndRet = hwndHost; - } - - pairCount++; - - if (pairCount > 1) - { - Log.Error("HideVolumeOSD: Multiple pairs found!"); - return IntPtr.Zero; - } + hWndInject = FindOSDWindow(); + + int count = 0; + + while (hWndInject == IntPtr.Zero && count < 10) + { + if (IsMuted) + { + keybd_event((byte)Keys.VolumeMute, 0, 0, 0); + System.Threading.Thread.Sleep(50); + keybd_event((byte)Keys.VolumeMute, 0, KEYEVENTF_KEYUP, 0); + + System.Threading.Thread.Sleep(200); + + keybd_event((byte)Keys.VolumeMute, 0, 0, 0); + System.Threading.Thread.Sleep(50); + keybd_event((byte)Keys.VolumeMute, 0, KEYEVENTF_KEYUP, 0); + } + else + { + keybd_event((byte)Keys.VolumeUp, 0, 0, 0); + System.Threading.Thread.Sleep(50); + keybd_event((byte)Keys.VolumeUp, 0, KEYEVENTF_KEYUP, 0); + + System.Threading.Thread.Sleep(200); + + keybd_event((byte)Keys.VolumeDown, 0, 0, 0); + System.Threading.Thread.Sleep(50); + keybd_event((byte)Keys.VolumeDown, 0, KEYEVENTF_KEYUP, 0); + } + + System.Threading.Thread.Sleep(500); + + hWndInject = FindOSDWindow(); + + count++; + } + + if (hWndInject == IntPtr.Zero) + { + Log.Error("HideVolumeOSD: VolumeOSD not found."); + } + } + + private IntPtr FindOSDWindow() + { + IntPtr hwndRet = IntPtr.Zero; + IntPtr hwndHost = IntPtr.Zero; + + // search for window with class 'NativeHWNDHost' + + int pairCount = 0; + + string strClass, strClassInner, strNameInner; + + if (Environment.OSVersion.Version.Build >= 22000) + { + //Windows 11 + strClass = "XamlExplorerHostIslandWindow"; + strClassInner = "Windows.UI.Composition.DesktopWindowContentBridge"; + strNameInner = "DesktopWindowXamlSource"; + } + else + { + strClass = "NativeHWNDHost"; + strClassInner = "DirectUIHWND"; + strNameInner = string.Empty; + } + + while ((hwndHost = FindWindowEx(IntPtr.Zero, hwndHost, strClass, "")) != IntPtr.Zero) + { + // if this window has a child with class 'DirectUIHWND' it should be the volume OSD + + if (FindWindowEx(hwndHost, IntPtr.Zero, strClassInner, strNameInner) != IntPtr.Zero) + { + if (pairCount == 0) + { + hwndRet = hwndHost; + } + + pairCount++; + + if (pairCount > 1) + { + Log.Error("HideVolumeOSD: Multiple pairs found!"); + return IntPtr.Zero; + } + } + } + + if (hwndRet == IntPtr.Zero) + { + Log.Error("HideVolumeOSD: OSD window not found!"); + } + + return hwndRet; + } + + public void HideOSD() + { + ShowWindow(hWndInject, 6); // SW_MINIMIZE + } + + public void ShowOSD() + { + ShowWindow(hWndInject, 9); // SW_RESTORE + // show window on the screen + + //Make sure, the Volume OSD is properly restored by invoking the presentation + keybd_event((byte)Keys.VolumeMute, 0, 0, 0); + System.Threading.Thread.Sleep(50); + keybd_event((byte)Keys.VolumeMute, 0, KEYEVENTF_KEYUP, 0); + + System.Threading.Thread.Sleep(200); + + keybd_event((byte)Keys.VolumeMute, 0, 0, 0); + System.Threading.Thread.Sleep(50); + keybd_event((byte)Keys.VolumeMute, 0, KEYEVENTF_KEYUP, 0); } - } - - if (hwndRet == IntPtr.Zero) - { - Log.Error("HideVolumeOSD: OSD window not found!"); - } - - return hwndRet; - } - - public void HideOSD() - { - ShowWindow(hWndInject, 6); // SW_MINIMIZE - } - - public void ShowOSD() - { - ShowWindow(hWndInject, 9); // SW_RESTORE - // show window on the screen - } - } + } }