public SpotifyHook() { RefreshTimer = new Timer((e) => { if (IsRunning()) { WindowName = Spotify.MainWindowTitle; Handle = Spotify.MainWindowHandle; if (VolumeControl == null) { VolumeControl = AudioUtils.GetVolumeControl(Children); } else { lastPeak = peak; peak = AudioUtils.GetPeakVolume(VolumeControl.Control); } } else { ClearHooks(); HookSpotify(); } }, null, TimeSpan.Zero, TimeSpan.FromMilliseconds(500)); }
public SpotifyState Check() { if (!Hooked) //if never or no more hooked, start hook { Hooked = HookSpotify(); } else if (!IsRunning()) // if hooked and spotify no more running, clean hook { CleanHook(); } else // else inspect spotify process { if (volumeControl != null) { lastPeak = peak; peak = AudioUtils.GetPeakVolume(volumeControl.Control); } else { volumeControl = AudioUtils.GetVolumeControl(children); } if (Spotify.HasExited) { handle = IntPtr.Zero; return(SpotifyState.NonHooked); } handle = Spotify.MainWindowHandle; var windowName = Spotify.MainWindowTitle; if (windowName.Equals("Spotify Free")) { return(SpotifyState.Pause); } if ((windowName.Equals("Advertisement") || windowName.Equals("Spotify")) && IsPlaying()) { return(SpotifyState.PlayingAd); } if (IsPlaying()) { return(SpotifyState.Playing); } } return(SpotifyState.NonHooked); }
private void RefreshTimer_Tick(object state) { if (IsRunning()) { WindowName = Spotify.MainWindowTitle; Handle = Spotify.MainWindowHandle; if (VolumeControl == null) { VolumeControl = AudioUtils.GetVolumeControl(Children); } else { lastPeak = peak; peak = AudioUtils.GetPeakVolume(VolumeControl.Control); } } else { ClearHooks(); HookSpotify(); } }
public bool IsPlaying() { return(AudioUtils.GetPeakVolume(VolumeControl) > 0); }
/** * Contains the logic for when to mute Spotify **/ private void MainTimer_Tick(object sender, EventArgs e) { try { if (hook.IsRunning()) { Debug.WriteLine(AudioUtils.GetPeakVolume(hook.VolumeControl)); if (hook.IsAdPlaying()) { if (MainTimer.Interval < 1500) { MainTimer.Interval = 1500; } if (!playingAd) { playingAd = true; } if (!muted) { Mute(true); } string artist = hook.GetArtist(); if (lastArtistName != artist) { StatusLabel.Text = "Muting: " + Truncate(artist); artistTooltip.SetToolTip(StatusLabel, lastArtistName = artist); LogAction("/mute/" + artist); } } else if (hook.IsPlaying()) // Normal music { if (muted) { Mute(false); } if (MainTimer.Interval > 400) { MainTimer.Interval = 400; } if (playingAd) { playingAd = false; } string artist = hook.GetArtist(); if (lastArtistName != artist) { StatusLabel.Text = "Playing: " + Truncate(artist); artistTooltip.SetToolTip(StatusLabel, lastArtistName = artist); LogAction("/play/" + artist); } } else { StatusLabel.Text = "Spotify is paused"; lastArtistName = ""; artistTooltip.SetToolTip(StatusLabel, ""); } } else { MainTimer.Interval = 5000; StatusLabel.Text = "Spotify is not running"; lastArtistName = ""; artistTooltip.SetToolTip(StatusLabel, ""); } } catch (Exception ex) { Debug.WriteLine(ex); } }