Skip to content

Commit

Permalink
Merge pull request #295 from MediaPortal/MP1-5211-Fix_initial_volume_…
Browse files Browse the repository at this point in the history
…mute_table_in_VolumeHandler

MP1-5211: Fix initial volume/mute/table in VolumeHandler
  • Loading branch information
andrewjswan authored Apr 1, 2024
2 parents a814076 + 3f10780 commit b0e001f
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 62 deletions.
4 changes: 4 additions & 0 deletions mediaportal/Core/Mixer/Mixer10.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 15 additions & 6 deletions mediaportal/Core/Player/VolumeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,18 @@ 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())
{
Expand Down Expand Up @@ -98,7 +107,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)
Expand Down Expand Up @@ -165,7 +174,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)
Expand Down Expand Up @@ -243,7 +252,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[]
Expand Down Expand Up @@ -296,7 +305,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[]
Expand Down Expand Up @@ -393,7 +402,7 @@ public virtual void UnMute()
}
}

private static int[] LoadFromRegistry()
protected static int[] LoadFromRegistry()
{
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Multimedia\Audio\VolumeControl"))
{
Expand Down
42 changes: 24 additions & 18 deletions mediaportal/Core/Player/VolumeHandlerCustom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System.Collections;
using MediaPortal.Configuration;
using MediaPortal.Profile;
using MediaPortal.GUI.Library;

namespace MediaPortal.Player
{
Expand All @@ -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
Expand Down
98 changes: 60 additions & 38 deletions mediaportal/Core/Util/HideVolumeOSDLib.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region Copyright (C) 2005-2016 Team MediaPortal
#region Copyright (C) 2005-2016 Team MediaPortal

// Copyright (C) 2005-2013 Team MediaPortal
// http://www.team-mediaportal.com
Expand Down Expand Up @@ -28,53 +28,69 @@

namespace HideVolumeOSD
{
public class HideVolumeOSDLib
{
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
public class HideVolumeOSDLib
{
private const int KEYEVENTF_KEYUP = 2;

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);

[DllImport("user32.dll", SetLastError = true)]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

IntPtr hWndInject = IntPtr.Zero;
[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;
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);

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

System.Threading.Thread.Sleep(500);
hWndInject = FindOSDWindow();

hWndInject = FindOSDWindow();
count++;
}

count++;
}

if (hWndInject == IntPtr.Zero)
{
Log.Error("HideVolumeOSD: VolumeOSD not found.");
}
}
}

private IntPtr FindOSDWindow()
{
private IntPtr FindOSDWindow()
{
IntPtr hwndRet = IntPtr.Zero;
IntPtr hwndHost = IntPtr.Zero;

Expand Down Expand Up @@ -125,22 +141,28 @@ private IntPtr FindOSDWindow()
}

return hwndRet;
}
}

public void HideOSD()
{
ShowWindow(hWndInject, 6); // SW_MINIMIZE
}
public void HideOSD()
{
ShowWindow(hWndInject, 6); // SW_MINIMIZE
}

public void ShowOSD()
{
ShowWindow(hWndInject, 9); // SW_RESTORE
// show window on the screen
// 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);
}
}
}

0 comments on commit b0e001f

Please sign in to comment.