Skip to content

Commit

Permalink
Version 1.8.1
Browse files Browse the repository at this point in the history
- Rewrite hook logic to fix a few bugs
  • Loading branch information
Xeroday committed Nov 7, 2021
1 parent d847738 commit c9e7e40
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 219 deletions.
18 changes: 18 additions & 0 deletions EZBlocker/EZBlocker/AudioUtils.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace EZBlocker
Expand All @@ -10,6 +11,23 @@ class AudioUtils
private const int MEDIA_PLAYPAUSE = 0xE0000;
private const int MEDIA_NEXTTRACK = 0xB0000;

public static void SetSpotifyMute(bool mute)
{
List<int> children = new List<int>();
foreach (Process p in Process.GetProcessesByName("spotify"))
{
children.Add(p.Id);
}
foreach (int child in children)
{
VolumeControl volumeControl = GetVolumeControl(child);
if (volumeControl != null)
{
SetMute(volumeControl.Control, mute);
}
}
}

public static void SendPlayPause(IntPtr target)
{
SendMessage(target, WM_APPCOMMAND, IntPtr.Zero, (IntPtr)MEDIA_PLAYPAUSE);
Expand Down
3 changes: 1 addition & 2 deletions EZBlocker/EZBlocker/EZBlocker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
<ItemGroup>
<Compile Include="AudioUtils.cs" />
<Compile Include="Analytics.cs" />
<Compile Include="Listener.cs" />
<Compile Include="MediaHook.cs" />
<Compile Include="Properties\strings.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
Expand All @@ -103,7 +103,6 @@
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SpotifyHook.cs" />
<EmbeddedResource Include="EZBlockerMain.resx">
<DependentUpon>EZBlockerMain.cs</DependentUpon>
<SubType>Designer</SubType>
Expand Down
32 changes: 13 additions & 19 deletions EZBlocker/EZBlocker/EZBlockerMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
using System.Threading;
using Microsoft.Win32;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Globalization;

namespace EZBlocker
{
Expand All @@ -30,7 +28,7 @@ public partial class Main : Form
private Analytics a;
private DateTime lastRequest;
private string lastAction = "";
private SpotifyHook hook;
private MediaHook hook;

public Main()
{
Expand All @@ -47,17 +45,17 @@ private void MainTimer_Tick(object sender, EventArgs e)
if (hook.IsRunning())
{
Debug.WriteLine("Is running");
if (hook.IsAdPlaying())
if (hook.IsAdPlaying)
{
if (MainTimer.Interval != 1000) MainTimer.Interval = 1000;
if (!muted) Mute(true);
if (!hook.IsPlaying())
if (!hook.IsPlaying)
{
AudioUtils.SendNextTrack(hook.Handle == IntPtr.Zero ? Handle : hook.Handle);
hook.SendNextTrack();
Thread.Sleep(500);
}

string artist = hook.GetArtist();
string artist = hook.CurrentArtist;
string message = Properties.strings.StatusMuting + " " + Truncate(artist);
if (lastMessage != message)
{
Expand All @@ -67,7 +65,7 @@ private void MainTimer_Tick(object sender, EventArgs e)
LogAction("/mute/" + artist);
}
}
else if (hook.IsPlaying()) // Normal music
else if (hook.IsPlaying) // Normal music
{
Debug.WriteLine("Playing");
if (muted)
Expand All @@ -77,7 +75,7 @@ private void MainTimer_Tick(object sender, EventArgs e)
}
if (MainTimer.Interval != 200) MainTimer.Interval = 200;

string artist = hook.GetArtist();
string artist = hook.CurrentArtist;
string message = Properties.strings.StatusPlaying + " " + Truncate(artist);
if (lastMessage != message)
{
Expand All @@ -87,7 +85,7 @@ private void MainTimer_Tick(object sender, EventArgs e)
LogAction("/play/" + artist);
}
}
else if (hook.WindowName.Equals("Spotify Free"))
else
{
string message = Properties.strings.StatusPaused;
if (lastMessage != message)
Expand Down Expand Up @@ -123,19 +121,15 @@ private void MainTimer_Tick(object sender, EventArgs e)
**/
private void Mute(bool mute)
{
hook.RefreshHooks();
foreach (AudioUtils.VolumeControl volumeControl in hook.VolumeControls)
{
AudioUtils.SetMute(volumeControl.Control, mute);
}
muted = hook.VolumeControls[0] != null && AudioUtils.IsMuted(hook.VolumeControls[0].Control) != null ? (bool)AudioUtils.IsMuted(hook.VolumeControls[0].Control) : true;
AudioUtils.SetSpotifyMute( mute);
muted = mute;
}

private string Truncate(string name)
{
if (name.Length > 10)
if (name.Length > 9)
{
return name.Substring(0, 10) + "...";
return name.Substring(0, 9) + "...";
}
return name;
}
Expand Down Expand Up @@ -246,7 +240,7 @@ private void Main_Load(object sender, EventArgs e)
a = new Analytics(Properties.Settings.Default.CID, Assembly.GetExecutingAssembly().GetName().Version.ToString());

// Start Spotify hook
hook = new SpotifyHook();
hook = new MediaHook();

MainTimer.Enabled = true;

Expand Down
56 changes: 0 additions & 56 deletions EZBlocker/EZBlocker/Listener.cs

This file was deleted.

83 changes: 83 additions & 0 deletions EZBlocker/EZBlocker/MediaHook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Windows.Media.Control;

namespace EZBlocker
{
class MediaHook
{
private readonly Timer RefreshTimer;
private GlobalSystemMediaTransportControlsSessionManager SessionManager;
private GlobalSystemMediaTransportControlsSession SpotifyMediaSession;
public string CurrentArtist { get; private set; }
public bool IsAdPlaying { get; private set; }
public bool IsPlaying { get; private set; }

public MediaHook()
{
RefreshTimer = new Timer((e) =>
{
_ = UpdateMediaInfo();
}, null, TimeSpan.Zero, TimeSpan.FromMilliseconds(2000));
}

private async Task RegisterSpotifyMediaSession()
{
if (SessionManager == null)
{
SessionManager = await GlobalSystemMediaTransportControlsSessionManager.RequestAsync();
}
foreach (GlobalSystemMediaTransportControlsSession session in SessionManager.GetSessions())
{
if (session.SourceAppUserModelId == "Spotify.exe")
{
Debug.WriteLine("Registering " + session.GetHashCode());
AudioUtils.SetSpotifyMute(false);
SpotifyMediaSession = session;
SpotifyMediaSession.MediaPropertiesChanged += async (s, args) =>
{
await UpdateMediaInfo();
};
return;
}
}
SpotifyMediaSession = null;
}

private async Task UpdateMediaInfo()
{
try
{
if (SpotifyMediaSession == null) throw new Exception();
GlobalSystemMediaTransportControlsSessionMediaProperties mediaProperties = await SpotifyMediaSession.TryGetMediaPropertiesAsync();
GlobalSystemMediaTransportControlsSessionPlaybackControls mediaControls = SpotifyMediaSession.GetPlaybackInfo().Controls;

CurrentArtist = mediaProperties.Artist.Length > 0 ? mediaProperties.Artist : mediaProperties.Title;
IsAdPlaying = !mediaControls.IsNextEnabled || mediaProperties.Title == "Advertisement";
IsPlaying = mediaControls.IsPauseEnabled;
} catch (Exception e)
{
Debug.WriteLine("UpdateMediaInfo exception " + e.ToString());
CurrentArtist = "N/A";
IsAdPlaying = false;
IsPlaying = false;
await RegisterSpotifyMediaSession();
}
}

public void SendNextTrack()
{
_ = SpotifyMediaSession.TryPlayAsync();
}

public bool IsRunning()
{
return (SpotifyMediaSession != null);
}
}
}
4 changes: 2 additions & 2 deletions EZBlocker/EZBlocker/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.8.0.0")]
[assembly: AssemblyFileVersion("1.8.0.0")]
[assembly: AssemblyVersion("1.8.1.0")]
[assembly: AssemblyFileVersion("1.8.1.0")]
Loading

0 comments on commit c9e7e40

Please sign in to comment.