Skip to content

Commit 5ccef6f

Browse files
aleabJohnnyCrazy
authored andcommitted
Raise OnTrackChange event for "other" tracks as well (#204)
* Raise OnTrackChange event for "other" tracks as well * Add null checks in Example project
1 parent aa8e940 commit 5ccef6f

3 files changed

Lines changed: 21 additions & 11 deletions

File tree

SpotifyAPI.Example/LocalControl.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,21 @@ public async void UpdateTrack(Track track)
8484
if (track.IsAd())
8585
return; //Don't process further, maybe null values
8686

87-
titleLinkLabel.Text = track.TrackResource.Name;
88-
titleLinkLabel.Tag = track.TrackResource.Uri;
87+
titleLinkLabel.Text = track.TrackResource?.Name;
88+
titleLinkLabel.Tag = track.TrackResource?.Uri;
8989

90-
artistLinkLabel.Text = track.ArtistResource.Name;
91-
artistLinkLabel.Tag = track.ArtistResource.Uri;
90+
artistLinkLabel.Text = track.ArtistResource?.Name;
91+
artistLinkLabel.Tag = track.ArtistResource?.Uri;
9292

93-
albumLinkLabel.Text = track.AlbumResource.Name;
94-
albumLinkLabel.Tag = track.AlbumResource.Uri;
93+
albumLinkLabel.Text = track.AlbumResource?.Name;
94+
albumLinkLabel.Tag = track.AlbumResource?.Uri;
9595

96-
SpotifyUri uri = track.TrackResource.ParseUri();
96+
SpotifyUri uri = track.TrackResource?.ParseUri();
9797

98-
trackInfoBox.Text = $@"Track Info - {uri.Id}";
98+
trackInfoBox.Text = $@"Track Info - {uri?.Id}";
9999

100-
bigAlbumPicture.Image = await track.GetAlbumArtAsync(AlbumArtSize.Size640);
101-
smallAlbumPicture.Image = await track.GetAlbumArtAsync(AlbumArtSize.Size160);
100+
bigAlbumPicture.Image = track.AlbumResource != null ? await track.GetAlbumArtAsync(AlbumArtSize.Size640) : null;
101+
smallAlbumPicture.Image = track.AlbumResource != null ? await track.GetAlbumArtAsync(AlbumArtSize.Size160) : null;
102102
}
103103

104104
public void UpdatePlayingStatus(bool playing)

SpotifyAPI/Local/Models/Track.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ public bool IsAd()
3838
return true;
3939
return false;
4040
}
41+
42+
/// <summary>
43+
/// Checks if the track id of type "other"
44+
/// </summary>
45+
/// <returns>true if the track is neither an advert nor a normal track, for example a podcast</returns>
46+
public bool IsOtherTrackType()
47+
{
48+
return TrackType == "other";
49+
}
4150

4251
/// <summary>
4352
/// Returns a URL to the album cover in the provided size

SpotifyAPI/Local/SpotifyLocalAPI.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ private void ElapsedTick(object sender, ElapsedEventArgs e)
107107
}
108108
if (newStatusResponse.Track != null && _eventStatusResponse.Track != null)
109109
{
110-
if (newStatusResponse.Track.TrackResource?.Uri != _eventStatusResponse.Track.TrackResource?.Uri)
110+
if (newStatusResponse.Track.TrackResource?.Uri != _eventStatusResponse.Track.TrackResource?.Uri ||
111+
newStatusResponse.Track.IsOtherTrackType() && newStatusResponse.Track.Length != this._eventStatusResponse.Track.Length)
111112
{
112113
OnTrackChange?.Invoke(this, new TrackChangeEventArgs()
113114
{

0 commit comments

Comments
 (0)