private void ElapsedTick(object sender, ElapsedEventArgs e) { if (_eventStatusResponse == null) { _eventStatusResponse = GetStatus(); _eventTimer.Start(); return; } StatusResponse newStatusResponse = GetStatus(); if (newStatusResponse == null) { _eventTimer.Start(); return; } if (!newStatusResponse.Running && newStatusResponse.Track == null) { _eventTimer.Start(); return; } if (newStatusResponse.Track != null && _eventStatusResponse.Track != null) { if (newStatusResponse.Track.TrackResource?.Uri != _eventStatusResponse.Track.TrackResource?.Uri || newStatusResponse.Track.IsOtherTrackType() && newStatusResponse.Track.Length != this._eventStatusResponse.Track.Length) { OnTrackChange?.Invoke(this, new TrackChangeEventArgs() { OldTrack = _eventStatusResponse.Track, NewTrack = newStatusResponse.Track }); } } if (newStatusResponse.Playing != _eventStatusResponse.Playing) { OnPlayStateChange?.Invoke(this, new PlayStateEventArgs() { Playing = newStatusResponse.Playing }); } if (newStatusResponse.Volume != _eventStatusResponse.Volume) { OnVolumeChange?.Invoke(this, new VolumeChangeEventArgs() { OldVolume = _eventStatusResponse.Volume, NewVolume = newStatusResponse.Volume }); } if (newStatusResponse.PlayingPosition != _eventStatusResponse.PlayingPosition) { OnTrackTimeChange?.Invoke(this, new TrackTimeChangeEventArgs() { TrackTime = newStatusResponse.PlayingPosition }); } _eventStatusResponse = newStatusResponse; _eventTimer.Start(); }
private void SpotifyOnTrackTimeChange(object sender, SpotifyAPI.Local.TrackTimeChangeEventArgs e) { var eventArgs = new TrackTimeChangeEventArgs() { TrackTime = e.TrackTime }; OnTrackTimeChange?.Invoke(this, eventArgs); }
public async void ElapsedEventTick(object sender, ElapsedEventArgs e) { SpotifyLatestStatus = await SpotifyProcess.GetSpotifyStatus(); if (SpotifyLatestStatus?.CurrentTrack == null) { EventTimer.Start(); return; } var newestTrack = SpotifyLatestStatus.CurrentTrack; if (Track != null) { if (newestTrack.Playing != Track.Playing) { if (newestTrack.Playing) { SongTimer.Start(); } else { SongTimer.Stop(); } await Task.Run(() => OnPlayStateChange?.Invoke(this, new PlayStateEventArgs() { Playing = newestTrack.Playing })); } if (!newestTrack.Equals(Track)) { SongTimer.Start(); await Task.Run(async() => OnTrackChange?.Invoke(this, new TrackChangeEventArgs() { OldTrack = Track, NewTrack = await SpotifyLatestStatus.GetTrack() })); } if (Track.CurrentPosition != null || newestTrack != null) { await Task.Run(() => OnTrackTimeChange?.Invoke(this, new TrackTimeChangeEventArgs() { TrackTime = newestTrack.Equals(Track) ? Track?.CurrentPosition ?? 0 : 0 })); } } if (newestTrack != null) { newestTrack.CurrentPosition = newestTrack.Equals(Track) ? Track?.CurrentPosition ?? 0 : (int?)null; Track = newestTrack; } EventTimer.Start(); }
protected void OnTrackTimeChangeHandler(int time) { OnTrackTimeChange?.Invoke(time); }
//*********************************************************************************************************************************************************************************************************** /// <summary> /// Check if an event has to be raised. /// </summary> private void ElapsedTick(object sender, ElapsedEventArgs e) { if (_tmpPlaybackStatus == null) { UpdateCurrentPlaybackStatus(); _tmpPlaybackStatus = CurrentPlaybackStatus; _eventTimer.Start(); return; } UpdateCurrentPlaybackStatus(); if (CurrentPlaybackStatus == null) { _eventTimer.Start(); return; } if (!CurrentPlaybackStatus.IsPlaying && CurrentPlaybackStatus.Track == null) { _eventTimer.Start(); return; } if (CurrentPlaybackStatus.IsPlaying && _tmpPlaybackStatus.Track != null && CurrentPlaybackStatus.Track == null) { OnTrackChange?.Invoke(this, new PlayerTrackChangeEventArgs() { OldTrack = _tmpPlaybackStatus.Track, NewTrack = null }); } if (CurrentPlaybackStatus.Track != null && _tmpPlaybackStatus.Track != null) { if (CurrentPlaybackStatus.Track?.TrackID != _tmpPlaybackStatus.Track?.TrackID || CurrentPlaybackStatus.Track.Duration != _tmpPlaybackStatus.Track.Duration) { OnTrackChange?.Invoke(this, new PlayerTrackChangeEventArgs() { OldTrack = _tmpPlaybackStatus.Track, NewTrack = CurrentPlaybackStatus.Track }); } } if (CurrentPlaybackStatus.IsPlaying != _tmpPlaybackStatus.IsPlaying) { OnPlayStateChange?.Invoke(this, new PlayerPlayStateEventArgs() { Playing = CurrentPlaybackStatus.IsPlaying }); } if (CurrentPlaybackStatus.DeviceVolumePercent != _tmpPlaybackStatus.DeviceVolumePercent) { OnVolumeChange?.Invoke(this, new PlayerVolumeChangeEventArgs() { OldVolume = _tmpPlaybackStatus.DeviceVolumePercent, NewVolume = CurrentPlaybackStatus.DeviceVolumePercent }); } if (CurrentPlaybackStatus.Progress != _tmpPlaybackStatus.Progress) { OnTrackTimeChange?.Invoke(this, new PlayerTrackTimeChangeEventArgs() { TrackTime = CurrentPlaybackStatus.Progress }); } _tmpPlaybackStatus = CurrentPlaybackStatus; _eventTimer.Start(); OnPropertyChanged("CurrentPlaybackStatus"); OnPropertyChanged("CurrentTrack"); }
private void OnLocalTrackTimeChange(object sender, SpotifyAPI.Local.TrackTimeChangeEventArgs e) { OnTrackTimeChange?.Invoke(sender, new TrackTimeChangeEventArgs(TimeSpan.FromSeconds(e.TrackTime))); }
public async Task TriggerEvents() { // avoid concurrences if (_processingEvents == true) { return; } _processingEvents = true; SpotifyLatestStatus = await SpotifyProcess.GetSpotifyStatus(); if (SpotifyLatestStatus?.CurrentTrack != null) { var newestTrack = SpotifyLatestStatus.CurrentTrack; if (Track != null) { if (newestTrack.Playing != Track.Playing) { if (newestTrack.Playing) { SongTimer?.Start(); } else { SongTimer?.Stop(); } _ = Task.Run(() => OnPlayStateChange?.Invoke(this, new PlayStateEventArgs() { Playing = newestTrack.Playing })); } if (!newestTrack.Equals(Track)) { SongTimer?.Start(); _ = Task.Run(async() => OnTrackChange?.Invoke(this, new TrackChangeEventArgs() { OldTrack = Track, NewTrack = await SpotifyLatestStatus.GetTrack() })); } if (Track.CurrentPosition != null || newestTrack != null) { _ = Task.Run(() => OnTrackTimeChange?.Invoke(this, new TrackTimeChangeEventArgs() { TrackTime = newestTrack.Equals(Track) ? Track?.CurrentPosition ?? 0 : 0 })); } } if (newestTrack != null) { newestTrack.CurrentPosition = newestTrack.Equals(Track) ? Track?.CurrentPosition ?? 0 : (int?)null; Track = newestTrack; } } EventTimerStart(); _processingEvents = false; }