/// <summary> /// Dispatches the <see cref="TrackStarted"/> event asynchronously. /// </summary> /// <param name="eventArgs">the event arguments passed with the event</param> /// <returns>a task that represents the asynchronous operation</returns> protected virtual Task OnTrackStartedAsync(TrackStartedEventArgs eventArgs) => TrackStarted.InvokeAsync(this, eventArgs);
public void InitializeSource(int minimumScrobbleSeconds, TrackStarted onTrackStartedCallback, TrackEnded onTrackEndedCallback) { _minimumScrobbleSeconds = minimumScrobbleSeconds; _onTrackStarted = onTrackStartedCallback; _onTrackEnded = onTrackEndedCallback; _isIntialized = true; try { _scrobbleTimer = new System.Timers.Timer(); _scrobbleTimer.Interval = 1000; _scrobbleTimer.Elapsed += async(o, e) => { _scrobbleTimer.Stop(); // Check for the iTunes process to ensure it's running. // If we don't check for it, the plugin would end up launching it, which we don't want Process[] wmpProcesses = Process.GetProcessesByName("wmplayer"); if (wmpProcesses.Length > 0 && _mediaPlayer == null) { _mediaPlayer = new WindowsMediaPlayer(); _mediaPlayer.Show(); Console.WriteLine("Windows Media Player Plugin successfully connected to the WMP COM library."); } else if (wmpProcesses.Length == 0 && _mediaPlayer != null) { //_mediaPlayer = null; Console.WriteLine("Windows Media Player process not detected. Waiting for Windows Media Player process to start..."); } if (_mediaPlayer != null) { Console.WriteLine("Windows Media Player Plugin checking media state..."); if (_isEnabled) { MediaItem mediaDetail = await GetMediaDetail(); if (mediaDetail != null && _mediaToScrobble.Count(mediaItem => mediaItem.TrackName == mediaDetail?.TrackName) == 0 && _currentMediaItem?.TrackName != mediaDetail?.TrackName) { if (_currentMediaItem != null) { _onTrackEnded?.Invoke(_currentMediaItem); } _currentMediaItem = mediaDetail; Console.WriteLine("Raising Track Change Method."); _onTrackStarted?.Invoke(mediaDetail); } if (_currentMediaItem != null) { Console.WriteLine($"Player position {_playerPosition} of {_currentMediaItem.TrackLength}."); if (mediaDetail != null && _mediaToScrobble.Count(item => item.TrackName == mediaDetail?.TrackName) == 0 && _playerPosition >= _minimumScrobbleSeconds && _playerPosition >= _currentMediaItem.TrackLength / 2 && mediaDetail?.TrackName != _lastQueuedItem?.TrackName) { _lastQueuedItem = mediaDetail; lock (_mediaLock) { _mediaToScrobble.Add(mediaDetail); Console.WriteLine($"Track {mediaDetail.TrackName} queued for Scrobbling."); } } } } Console.WriteLine("Windows Media Plugin checking media state complete."); } _scrobbleTimer.Start(); }; } catch (Exception ex) { _scrobbleTimer.Start(); } }
internal Task OnTrackStartedAsync(TrackStartedEventArgs eventArgs) => TrackStarted.InvokeAsync(this, eventArgs);
public void InitializeSource(int minimumScrobbleSeconds, TrackStarted onTrackStartedCallback, TrackEnded onTrackEndedCallback, ScrobbleTrack onScrobbleTrack) { _minimumScrobbleSeconds = minimumScrobbleSeconds; _onTrackStarted = onTrackStartedCallback; _onTrackEnded = onTrackEndedCallback; _onScrobbleTrack = onScrobbleTrack; _isIntialized = true; try { _scrobbleTimer = new Timer(); _scrobbleTimer.Interval = 1000; _scrobbleTimer.Elapsed += async(o, e) => { _scrobbleTimer.Stop(); iTunesApp iTunesApp = null; // Check for the iTunes process to ensure it's running. // If we don't check for it, the plugin would end up launching it, which we don't want Process[] iTunesProcesses = Process.GetProcessesByName("iTunes"); if (iTunesProcesses.Length > 0) { try { iTunesApp = new iTunesApp(); Console.WriteLine("iTunes Plugin successfully connected to iTunes COM library."); } catch (Exception) { // Ignore this, more than likely the application was in the process of closing while we tried to read from it } } else if (iTunesProcesses.Length == 0 && _currentMediaItem != null) { _onTrackEnded(null); _currentMediaItem = null; Console.WriteLine("iTunes process not detected. Waiting for iTunes process to start..."); } if (iTunesApp != null) { Console.WriteLine("iTunes Plugin checking media state..."); if (_isEnabled) { MediaItem mediaDetail = await GetMediaDetail(iTunesApp); if (mediaDetail != null && _mediaToScrobble.Count(mediaItem => mediaItem.TrackName == mediaDetail?.TrackName) == 0 && _currentMediaItem?.TrackName != mediaDetail?.TrackName && iTunesApp.PlayerState == ITPlayerState.ITPlayerStatePlaying) { _currentMediaPlayTime = 1; if (_currentMediaItem != null) { _onTrackEnded?.Invoke(_currentMediaItem); } _currentMediaItem = mediaDetail; _lastStatePaused = false; Console.WriteLine("Raising Track Change Method."); _onTrackStarted?.Invoke(mediaDetail, false); mediaDetail.StartedPlaying = DateTime.Now; } else if (iTunesApp.PlayerState != ITPlayerState.ITPlayerStatePlaying) { if (_currentMediaPlayTime > 0) { _onTrackEnded?.Invoke(_currentMediaItem); _currentMediaItem = null; } if (iTunesApp.PlayerState != ITPlayerState.ITPlayerStateStopped) { _lastStatePaused = false; _currentMediaPlayTime = 0; } } else if (iTunesApp.PlayerState == ITPlayerState.ITPlayerStatePlaying && _currentMediaItem?.TrackName == mediaDetail?.TrackName) { if (_currentMediaPlayTime == 0 || _lastStatePaused) { _onTrackStarted?.Invoke(_currentMediaItem, _lastStatePaused); } _currentMediaPlayTime++; } if (_currentMediaItem != null) { Console.WriteLine($"Current media playing time: {_currentMediaPlayTime} of {_currentMediaItem.TrackLength}."); if (mediaDetail != null && _mediaToScrobble.Count(item => item.TrackName == mediaDetail?.TrackName) == 0 && _currentMediaPlayTime >= _minimumScrobbleSeconds && _currentMediaPlayTime >= Math.Min(_currentMediaItem.TrackLength / 2, 5 * 60) && mediaDetail?.TrackName != _lastQueuedItem?.TrackName) { _lastQueuedItem = mediaDetail; lock (_mediaLock) { _mediaToScrobble.Add(mediaDetail); Console.WriteLine($"Track {mediaDetail.TrackName} queued for Scrobbling."); } _onScrobbleTrack?.Invoke(mediaDetail); } } } Console.WriteLine("iTunes Plugin checking media state complete."); } else if (_currentMediaItem != null) { _onTrackEnded?.Invoke(_currentMediaItem); _currentMediaItem = null; } if (iTunesApp != null) { Marshal.ReleaseComObject(iTunesApp); } _scrobbleTimer.Start(); }; } catch (Exception ex) { _scrobbleTimer.Start(); } }