/// <summary> /// Track skipped event handler /// </summary> /// <param name="info">TrackInfo object</param> public void TrackSkipped(TrackInfo info) { RatingDelta delta = null; if (countSkipped > 0) { delta = RatingUpdate.TrackNextSkipped; } else { if (info.MinimumDelta > info.Duration / 2) { delta = RatingUpdate.TrackSkipped; countSkipped = 1; } else { delta = RatingUpdate.TrackHalfPlayed; countSkipped = 0; // little hack, to give less points to next played track // track half-played, substact less points } } if ((ShuffleStatistics.SkipsSinceShuffle == -1) && (ShuffleStatistics.SkipsSinceShuffle > 0)) { ShuffleStatistics.SkipsSinceShuffle = 1; } else { ShuffleStatistics.SkipsSinceShuffle++; } ds.AcceptChanges(); this.TrackEvent(info.Artist, info.Album, info.Name, info.Duration, info.Genre, info.TrackID, delta); }
public void LoadPlaylist() { try { List<TrackInfo> list = new List<TrackInfo>(); progress = 0; IWMPPlaylist pls = null; bool playlistFound = true; if (Parameters.UsePlaylist) { IWMPPlaylistArray plsArr = wmp.playlistCollection.getByName(Parameters.SourcePlaylist); if (plsArr.count > 0) { pls = plsArr.Item(0); } else { playlistFound = false; pls = wmp.playlistCollection.newPlaylist(Parameters.SourcePlaylist); IWMPPlaylist s = wmp.mediaCollection.getAll(); int cnt = s.count; String a = String.Empty; for (int i = 0; i < cnt; i++) { String mediaType = String.Empty; IWMPMedia media = s.get_Item(i); try { mediaType = media.getItemInfo(Consts.Attributes.MediaType); } catch (Exception) { mediaType = String.Empty; } if ((mediaType.Equals(Consts.MediaType.Audio)) || ((!(Parameters.SkipNonAudio)) && (mediaType.Equals(Consts.MediaType.Video)))) { pls.appendItem(s.get_Item(i)); } progress = 25 * i / cnt; } //Log.Write(a); } } else { pls = wmp.mediaCollection.getAll(); } int current = 0; int count = pls.count; for (int i = 0; i < count; i++) { IWMPMedia media = pls.get_Item(i); String cTrackID = String.Empty; String cArtist = String.Empty; String cAlbum = String.Empty; String cName = String.Empty; String cGenre = String.Empty; String mediaType = String.Empty; int cDuration = 0; try { mediaType = media.getItemInfo(Consts.Attributes.MediaType); } catch (Exception) { } try { cTrackID = media.getItemInfo(Consts.Attributes.TrackID); } catch (Exception) { } try { cArtist = media.getItemInfo(Consts.Attributes.Arist); } catch (Exception) { } try { cAlbum = media.getItemInfo(Consts.Attributes.Album); } catch (Exception) { } try { cName = media.getItemInfo(Consts.Attributes.Name); } catch (Exception) { } try { cGenre = media.getItemInfo(Consts.Attributes.Genre); } catch (Exception) { } try { cDuration = Int32.Parse(media.getItemInfo(Consts.Attributes.Duration)); } catch (Exception) { } if ((mediaType.Equals(Consts.MediaType.Audio)) || ((!(Parameters.SkipNonAudio)) && (mediaType.Equals(Consts.MediaType.Video)))) { TrackInfo trackInfo = new TrackInfo(cName, cArtist, cAlbum, cGenre, cTrackID, cDuration, 0); try { trackInfo.Rating = keeper.GetRating(trackInfo); } catch (RatingNotFoundException) { trackInfo.Rating = 0; } trackInfo.TrackObject = media; list.Add(trackInfo); } if (playlistFound) { progress = current * 50 / count; } else { progress = 25 + current * 25 / count; } current++; } progress = 50; playlist = list; } catch (Exception e) { Log.Write(e); throw e; } }
/// <summary> /// Track replayed event handler /// </summary> /// <param name="info">TrackInfo object</param> public void TrackReplayed(TrackInfo info) { RatingDelta delta = null; if (countSkipped > 0) { delta = RatingUpdate.TrackAfterSkip; countSkipped = 0; } else if (countSkipped == 0) { delta = RatingUpdate.TrackNextAfterSkip; countSkipped = -1; } else { delta = RatingUpdate.TrackReplayed; } ShuffleStatistics.PlaysSinceShuffle++; ds.AcceptChanges(); this.TrackEvent(info.Artist, info.Album, info.Name, info.Duration, info.Genre, info.TrackID, delta); }
/// <summary> /// Get track rating /// Could throw RatingNotFoundException when no information in rating database /// </summary> /// <param name="trackInfo">TrackInfo object</param> /// <returns>Track rating</returns> public int GetRating(TrackInfo trackInfo) { if (trackInfo.Name == null) { trackInfo.Name = String.Empty; } if (trackInfo.Artist == null) { trackInfo.Artist = String.Empty; } if (trackInfo.Album == null) { trackInfo.Album = String.Empty; } if (trackInfo.Genre == null) { trackInfo.Genre = String.Empty; } MediaDataSet.TracksRow track = ds.Tracks.SingleOrDefault(s => ((s.Name.Equals(trackInfo.Name)) && (s.AlbumsRow.Name.Equals(trackInfo.Album)) && (s.ArtistsRow.Name.Equals(trackInfo.Artist)))); if (track != null) { trackInfo.RatingState = TrackRatingState.Track; return track.Rating + track.ArtistsRow.Rating + track.AlbumsRow.Rating + track.GenresRow.Rating; } else { // need to check cumulative rating bool found = false; int rating = 0; trackInfo.RatingState = TrackRatingState.NotRated; MediaDataSet.GenresRow genre = ds.Genres.SingleOrDefault(s => (s.Name.Equals(trackInfo.Genre))); if (genre != null) { found = true; rating += genre.Rating; trackInfo.RatingState = TrackRatingState.Genre; } MediaDataSet.ArtistsRow artist = ds.Artists.SingleOrDefault(s => (s.Name.Equals(trackInfo.Artist))); if (artist != null) { found = true; rating += artist.Rating; trackInfo.RatingState = TrackRatingState.Artist; } MediaDataSet.AlbumsRow album = ds.Albums.SingleOrDefault(s => ((s.Name.Equals(trackInfo.Album)) && (s.ArtistsRow.Name.Equals(trackInfo.Artist)))); if (album != null) { found = true; rating += album.Rating; trackInfo.RatingState = TrackRatingState.Album; } if (found) { return rating; } else { throw new RatingNotFoundException(); } } }
private void checkPos() { if (UtilityLib.Consts.Locales.EnglishCulture.NativeName.ToLower().Equals(Parameters.Language)) { System.Threading.Thread.CurrentThread.CurrentUICulture = UtilityLib.Consts.Locales.EnglishCulture; } else if (UtilityLib.Consts.Locales.RussianCulture.NativeName.ToLower().Equals(Parameters.Language)) { System.Threading.Thread.CurrentThread.CurrentUICulture = UtilityLib.Consts.Locales.RussianCulture; } else { System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture; } if (!(quit)) { this.RecalculateStatistics(); } state = MonitorState.Started; while (true) { lock (lckThread) { if (quit) return; } try { if (wmp.currentMedia != null) { if (playingTrack != null) { // checking if we're replaying... if (playingTrack.Position <= MediaInfoLib.Consts.ReplayPosition) { if (playingTrack.MinimumDelta < (playingTrack.Duration / 4)) { // this means, that we've listened for more than 3/4 of the song keeper.TrackReplayed(playingTrack); playingTrack.MinimumDelta = playingTrack.Duration - playingTrack.Position; lock (lckTracks) { if (String.IsNullOrEmpty(playingTrack.Album)) { trackStatus = String.Format(MediaInfoLib.Resources.InterfaceLabels.TrackReplayedFormat, playingTrack.Artist, playingTrack.Name, String.Empty); } else { trackStatus = String.Format(MediaInfoLib.Resources.InterfaceLabels.TrackReplayedFormat, playingTrack.Artist, playingTrack.Name, String.Format(MediaInfoLib.Resources.InterfaceLabels.AlbumFormat, playingTrack.Album)); } } } } } String cTrackID = String.Empty; String cArtist = String.Empty; String cAlbum = String.Empty; String cName = String.Empty; String cGenre = String.Empty; int cDuration = 0; int cPosition = 0; IWMPMedia current = wmp.currentMedia; try { cTrackID = current.getItemInfo(Consts.Attributes.TrackID); } catch (Exception) { } try { cArtist = current.getItemInfo(Consts.Attributes.Arist); } catch (Exception) { } try { cAlbum = current.getItemInfo(Consts.Attributes.Album); } catch (Exception) { } try { cName = current.getItemInfo(Consts.Attributes.Name); } catch (Exception) { } try { cGenre = current.getItemInfo(Consts.Attributes.Genre); } catch (Exception) { } try { cDuration = (int)Double.Parse(current.getItemInfo(Consts.Attributes.Duration)); } catch (Exception) { } try { cPosition = (int)wmp.controls.currentPosition; } catch (Exception) { } if (!(cTrackID.Equals(trackID))) { trackID = cTrackID; if (playingTrack != null) { if (playingTrack.MinimumDelta > Consts.LastSecondsDelta) { // track definetely was skipped keeper.TrackSkipped(playingTrack); lock (lckTracks) { if (String.IsNullOrEmpty(playingTrack.Album)) { trackStatus = String.Format(MediaInfoLib.Resources.InterfaceLabels.TrackSkippedFormat, playingTrack.Artist, playingTrack.Name, String.Empty); } else { trackStatus = String.Format(MediaInfoLib.Resources.InterfaceLabels.TrackSkippedFormat, playingTrack.Artist, playingTrack.Name, String.Format(MediaInfoLib.Resources.InterfaceLabels.AlbumFormat, playingTrack.Album)); } } } else { // track played till the end keeper.TrackPlayed(playingTrack); lock (lckTracks) { if (String.IsNullOrEmpty(playingTrack.Album)) { trackStatus = String.Format(MediaInfoLib.Resources.InterfaceLabels.TrackPlayedFormat, playingTrack.Artist, playingTrack.Name, String.Empty); } else { trackStatus = String.Format(MediaInfoLib.Resources.InterfaceLabels.TrackPlayedFormat, playingTrack.Artist, playingTrack.Name, String.Format(MediaInfoLib.Resources.InterfaceLabels.AlbumFormat, playingTrack.Album)); } } } keeper.Save(); keeper.SaveSummary(); } playingTrack = new TrackInfo(cName, cArtist, cAlbum, cGenre, cTrackID, cDuration, cPosition); } playingTrack.Position = cPosition; if ((playingTrack.Duration - cPosition) < playingTrack.MinimumDelta) { playingTrack.MinimumDelta = playingTrack.Duration - cPosition; } lock (lckTracks) { trackCurrent = String.Format(MediaInfoLib.Resources.InterfaceLabels.CurrentTrackFormat, cArtist, cName); trackAlbum = cAlbum; } } } catch (COMException) { // application is busy... nothing we can do... just wait, when it will become available again } catch (Exception e) { // log exeptions Log.Write(e); lock (lckTracks) { trackStatus = String.Format(MediaInfoLib.Resources.InterfaceLabels.InterfaceErrorFormat, e.Message); } } Thread.Sleep(1000); } }
public void LoadPlaylist() { try { List<TrackInfo> list = new List<TrackInfo>(); IITPlaylist source = null; bool playlistFound = true; progress = 0; if (Parameters.UsePlaylist) { // looking for playlist foreach (IITSource src in iTunes.Sources) { foreach (IITPlaylist pls in src.Playlists) { if (pls.Name.Equals(Parameters.SourcePlaylist)) { source = pls; break; } } } if (source == null) { // creating source playlist and loading it with library contents IITUserPlaylist pl = iTunes.CreatePlaylist(Parameters.SourcePlaylist) as IITUserPlaylist; playlistFound = false; progress = 0; int curr = 0; int cnt = iTunes.LibraryPlaylist.Tracks.Count; foreach (IITTrack trk in iTunes.LibraryPlaylist.Tracks) { if ((trk.SampleRate != 0) || (!(Parameters.SkipNonAudio))) { object trkObj = trk; pl.AddTrack(ref trkObj); } progress = curr * 25 / cnt; curr++; } source = pl; } } else { source = iTunes.LibraryPlaylist; } int total = source.Tracks.Count; int current = 0; foreach (IITTrack track in source.Tracks) { if ((track.SampleRate != 0) || (!(Parameters.SkipNonAudio))) { TrackInfo trackInfo = new TrackInfo(track.Name, track.Artist, track.Album, track.Genre, track.trackID.ToString(), track.Duration, 0); try { trackInfo.Rating = keeper.GetRating(trackInfo); } catch (RatingNotFoundException) { trackInfo.Rating = 0; } trackInfo.TrackObject = track; list.Add(trackInfo); } if (playlistFound) { progress = current * 50 / total; } else { progress = 25 + current * 25 / total; } current++; } progress = 50; playlist = list; } catch (Exception e) { Log.Write(e); throw e; } }
private void checkPos() { if (UtilityLib.Consts.Locales.EnglishCulture.NativeName.ToLower().Equals(Parameters.Language)) { System.Threading.Thread.CurrentThread.CurrentUICulture = UtilityLib.Consts.Locales.EnglishCulture; } else if (UtilityLib.Consts.Locales.RussianCulture.NativeName.ToLower().Equals(Parameters.Language)) { System.Threading.Thread.CurrentThread.CurrentUICulture = UtilityLib.Consts.Locales.RussianCulture; } else { System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture; } try { iTunes = new iTunesAppClass(); if (!(quit)) { this.RecalculateStatistics(); } } catch (Exception e) { Log.Write(e); errorMessage = "Error contacting player: " + e.Message; state = MonitorState.Error; return; } state = MonitorState.Started; while (true) { lock (lckThread) { if (quit) return; } try { if (iTunes.CurrentTrack != null) { if (playingTrack != null) { // checking if we're replaying... if (playingTrack.Position <= MediaInfoLib.Consts.ReplayPosition) { if (playingTrack.MinimumDelta < (playingTrack.Duration / 4)) { // this means, that we've listened for more than 3/4 of the song keeper.TrackReplayed(playingTrack); playingTrack.MinimumDelta = playingTrack.Duration - playingTrack.Position; } } } if (trackID != iTunes.CurrentTrack.trackID) { trackID = iTunes.CurrentTrack.trackID; if (playingTrack != null) { if (playingTrack.MinimumDelta > Consts.LastSecondsDelta) { // track definetely was skipped keeper.TrackSkipped(playingTrack); lock (lckTracks) { if (String.IsNullOrEmpty(playingTrack.Album)) { trackStatus = String.Format(MediaInfoLib.Resources.InterfaceLabels.TrackSkippedFormat, playingTrack.Artist, playingTrack.Name, String.Empty); } else { trackStatus = String.Format(MediaInfoLib.Resources.InterfaceLabels.TrackSkippedFormat, playingTrack.Artist, playingTrack.Name, String.Format(MediaInfoLib.Resources.InterfaceLabels.AlbumFormat, playingTrack.Album)); } } } else { // track played till the end keeper.TrackPlayed(playingTrack); lock (lckTracks) { if (String.IsNullOrEmpty(playingTrack.Album)) { trackStatus = String.Format(MediaInfoLib.Resources.InterfaceLabels.TrackPlayedFormat, playingTrack.Artist, playingTrack.Name, String.Empty); } else { trackStatus = String.Format(MediaInfoLib.Resources.InterfaceLabels.TrackPlayedFormat, playingTrack.Artist, playingTrack.Name, String.Format(MediaInfoLib.Resources.InterfaceLabels.AlbumFormat, playingTrack.Album)); } } } keeper.Save(); keeper.SaveSummary(); } playingTrack = new TrackInfo(iTunes.CurrentTrack.Name, iTunes.CurrentTrack.Artist, iTunes.CurrentTrack.Album, iTunes.CurrentTrack.Genre, iTunes.CurrentTrack.trackID.ToString(), iTunes.CurrentTrack.Duration, iTunes.PlayerPosition); } playingTrack.Position = iTunes.PlayerPosition; if ((playingTrack.Duration - playingTrack.Position) < playingTrack.MinimumDelta) { playingTrack.MinimumDelta = playingTrack.Duration - playingTrack.Position; } lock (lckTracks) { trackCurrent = String.Format(MediaInfoLib.Resources.InterfaceLabels.CurrentTrackFormat, iTunes.CurrentTrack.Artist, iTunes.CurrentTrack.Name); trackAlbum = iTunes.CurrentTrack.Album; } } } catch (InvalidCastException e) { // COM server is down, application will need to reinitialize // iTunes = new iTunesAppClass(); //trackLog = String.Format("iTunes is closed, closing iPlaylist monitor!{0}", Environment.NewLine) + trackLog; lock (lckTracks) { errorMessage = "COM server shutting down error: " + e.Message; state = MonitorState.Interrupted; } return; } catch (COMException) { // application is busy... nothing we can do... just wait, when it will become available again } catch (Exception e) { Log.Write(e); // log exeptions lock (lckTracks) { trackStatus = String.Format(MediaInfoLib.Resources.InterfaceLabels.InterfaceErrorFormat, e.Message); } } Thread.Sleep(1000); } }