diff --git a/EZBlocker/EZBlocker/AudioUtils.cs b/EZBlocker/EZBlocker/AudioUtils.cs index c026840..044fcda 100755 --- a/EZBlocker/EZBlocker/AudioUtils.cs +++ b/EZBlocker/EZBlocker/AudioUtils.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Runtime.InteropServices; namespace EZBlocker @@ -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 children = new List(); + 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); diff --git a/EZBlocker/EZBlocker/EZBlocker.csproj b/EZBlocker/EZBlocker/EZBlocker.csproj index 4d9bf6f..e5d80db 100755 --- a/EZBlocker/EZBlocker/EZBlocker.csproj +++ b/EZBlocker/EZBlocker/EZBlocker.csproj @@ -88,7 +88,7 @@ - + True True @@ -103,7 +103,6 @@ - EZBlockerMain.cs Designer diff --git a/EZBlocker/EZBlocker/EZBlockerMain.cs b/EZBlocker/EZBlocker/EZBlockerMain.cs index 5c4a562..a2ffe4a 100755 --- a/EZBlocker/EZBlocker/EZBlockerMain.cs +++ b/EZBlocker/EZBlocker/EZBlockerMain.cs @@ -9,8 +9,6 @@ using System.Threading; using Microsoft.Win32; using System.Threading.Tasks; -using System.Collections.ObjectModel; -using System.Globalization; namespace EZBlocker { @@ -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() { @@ -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) { @@ -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) @@ -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) { @@ -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) @@ -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; } @@ -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; diff --git a/EZBlocker/EZBlocker/Listener.cs b/EZBlocker/EZBlocker/Listener.cs deleted file mode 100755 index 7133adf..0000000 --- a/EZBlocker/EZBlocker/Listener.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Net; - -namespace EZBlocker -{ - class Listener - { - HttpListener listener; - private bool enabled; - private const string endpoint = "http://localhost:19691/"; - - public string Message { get; private set; } = ""; - - public Listener() - { - listener = new HttpListener(); - listener.Prefixes.Add(endpoint); - listener.Start(); - - enabled = true; - } - - public void Listen() - { - while (enabled) - { - try - { - HttpListenerContext context = listener.GetContext(); - Handle(context.Request, context.Response); - } catch (Exception) { } - } - } - - public void Stop() - { - enabled = false; - listener.Stop(); - } - - private void Handle(HttpListenerRequest request, HttpListenerResponse response) - { - string path = request.Url.LocalPath.Substring(1); - if (!path.Equals(Message)) - { - Message = path; - } - - byte[] buffer = System.Text.Encoding.UTF8.GetBytes("1"); - response.ContentLength64 = buffer.Length; - System.IO.Stream output = response.OutputStream; - output.Write(buffer, 0, buffer.Length); - output.Close(); - } - } -} diff --git a/EZBlocker/EZBlocker/MediaHook.cs b/EZBlocker/EZBlocker/MediaHook.cs new file mode 100755 index 0000000..97613e6 --- /dev/null +++ b/EZBlocker/EZBlocker/MediaHook.cs @@ -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); + } + } +} diff --git a/EZBlocker/EZBlocker/Properties/AssemblyInfo.cs b/EZBlocker/EZBlocker/Properties/AssemblyInfo.cs index c1ff5fe..efc64f4 100755 --- a/EZBlocker/EZBlocker/Properties/AssemblyInfo.cs +++ b/EZBlocker/EZBlocker/Properties/AssemblyInfo.cs @@ -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")] diff --git a/EZBlocker/EZBlocker/SpotifyHook.cs b/EZBlocker/EZBlocker/SpotifyHook.cs deleted file mode 100755 index 0a3713b..0000000 --- a/EZBlocker/EZBlocker/SpotifyHook.cs +++ /dev/null @@ -1,140 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Runtime.InteropServices; -using System.Threading; -using Windows.Media.Control; - -namespace EZBlocker -{ - class SpotifyHook - { - public Process Spotify { get; private set; } - private List Children; - public List VolumeControls { get; private set; } - public string WindowName { get; private set; } - public IntPtr Handle { get; private set; } - - private GlobalSystemMediaTransportControlsSession gsmtcs; - private bool IsNextEnabled = true; - - private readonly Timer RefreshTimer; - - public SpotifyHook() - { - RefreshTimer = new Timer((e) => - { - if (IsRunning() && gsmtcs != null) - { - WindowName = Spotify.MainWindowTitle; - Handle = Spotify.MainWindowHandle; - IsNextEnabled = gsmtcs.GetPlaybackInfo().Controls.IsNextEnabled; - Debug.WriteLine("Hook: Next " + IsNextEnabled); - } - else - { - RefreshHooks(); - } - }, null, TimeSpan.Zero, TimeSpan.FromMilliseconds(300)); - } - - public bool IsPlaying() - { - Debug.WriteLine("Hook: " + gsmtcs.GetPlaybackInfo().PlaybackStatus); - return gsmtcs.GetPlaybackInfo().PlaybackStatus == GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing; - } - - public bool IsAdPlaying() - { - if (WindowName.Equals("Advertisement") || !IsNextEnabled && IsPlaying()) - { - Debug.WriteLine("Hook: " + "IsAdPlaying true"); - return true; - } - return false; - } - - public bool IsRunning() - { - if (Spotify == null) - return false; - - Spotify.Refresh(); - return !Spotify.HasExited; - } - - public string GetArtist() - { - if (IsPlaying()) - { - if (WindowName.Contains(" - ")) - return WindowName.Split(new[] { " - " }, StringSplitOptions.None)[0]; - else - return WindowName; - } - - return ""; - } - - private void ClearHooks() - { - Spotify = null; - WindowName = ""; - Handle = IntPtr.Zero; - if (VolumeControls != null) - { - foreach (AudioUtils.VolumeControl volumeControl in VolumeControls) - { - Marshal.ReleaseComObject(volumeControl.Control); - } - VolumeControls = null; - } - gsmtcs = null; - } - - private bool HookSpotify() - { - // Hook windows media controller - try - { - gsmtcs = GlobalSystemMediaTransportControlsSessionManager.RequestAsync().GetAwaiter().GetResult().GetCurrentSession(); - } catch (Exception e) - { - Debug.WriteLine("Hook: " + e); - } - - // Hook processes - Children = new List(); - foreach (Process p in Process.GetProcessesByName("spotify")) - { - Children.Add(p.Id); - if (p.MainWindowTitle.Length > 1 && Spotify == null) - { - Spotify = p; - } - } - - // Hook audio controls - VolumeControls = new List(); - foreach (int child in Children) - { - AudioUtils.VolumeControl volumeControl = AudioUtils.GetVolumeControl(child); - if (volumeControl != null) - { - if (Spotify == null) Spotify = Process.GetProcessById(volumeControl.ProcessId); - VolumeControls.Add(volumeControl); - } - } - - if (VolumeControls.Count > 0) return true; - - return false; - } - - public void RefreshHooks() - { - ClearHooks(); - HookSpotify(); - } - } -}