private void timerScanAd_Tick(object sender, EventArgs e) { if (HideForm) { HideForm = false; this.Hide(); } lblStatus.Text = "Scanning Spotify..."; var processes = Process.GetProcessesByName("spotify"); if (processes.Length <= 0) { return; } if (ProcessID != processes[0].Id) { ProcessID = processes[0].Id; } float?volume = SoundControl.GetApplicationVolume((uint)ProcessID); if (volume == null) { return; } bool mute = volume <= (1f / 100f); var title = processes[0].MainWindowTitle; var data = title.Split(new[] { " - " }, StringSplitOptions.RemoveEmptyEntries); if (data.Length <= 1) { return; } var songdata = string.Join(" - ", data, 1, data.Length - 1).Split(new[] { " – " }, StringSplitOptions.RemoveEmptyEntries); var adstatus = CheckSongIsAd(songdata[0], songdata[1]); if (adstatus && !mute) { OriginalVolume = (float)volume; SoundControl.SetApplicationVolume((uint)ProcessID, 1f / 1000f); Thread.Sleep(100); //Force a play after volume is set as Spotify will stop the ad. PostMessage(processes[0].MainWindowHandle, 0x319, IntPtr.Zero, new IntPtr(0xE0000L)); lblStatus.Text = "Ad detected, muting."; notifyIcon.ShowBalloonTip(200, "Spotify Ad Blocker", "Muting Spotify, ad detected.", ToolTipIcon.None); } else if (!adstatus && mute) { if (OriginalVolume == 0f) { OriginalVolume = 1f; } SoundControl.SetApplicationVolume((uint)ProcessID, OriginalVolume); lblStatus.Text = "Ad not detected."; notifyIcon.ShowBalloonTip(200, "Spotify Ad Blocker", "Ad no longer detected, unmuting Spotify.", ToolTipIcon.None); } }
private void resetVolumeToolStripMenuItem_Click(object sender, EventArgs e) { SoundControl.SetApplicationVolume((uint)ProcessID, OriginalVolume); notifyIcon.ShowBalloonTip(1000, "Spotify Ad Blocker", "Resetting volume to original...", ToolTipIcon.None); }