Skip to content

Commit

Permalink
Retry iTunes album art retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
hexawyz committed Oct 23, 2021
1 parent dabd3ee commit 36a6a45
Showing 1 changed file with 49 additions and 25 deletions.
74 changes: 49 additions & 25 deletions Snip/Players/iTunes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@
namespace Winter
{
using System.Globalization;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.Windows.Forms;
using iTunesLib;

internal sealed class Itunes : MediaPlayer
{
private iTunesApp ItunesApplication = null;

// This will count the number of times the state changed. Used to retry getting Album Art.
private int StateVersion;

// This will hold the volume prior to it being muted and restored from it.
private int muteVolume = 0;
Expand Down Expand Up @@ -118,13 +122,17 @@ public override void StopTrack()
this.ItunesApplication.Stop();
}

private void App_OnPlayerPlayEvent(object sender)
// Sometimes, iTunes will not reurn the Album Art timely, so retrying will avoid keeping an empty album art.
private async Task UpdateAlbumArtAsync(int version, IITTrack track)
{
IITTrack track = this.ItunesApplication.CurrentTrack;
// Retry for up to 5sec
const int retryCount = 50;
int i = 0;
const int retryDelay = 100;

if (!string.IsNullOrEmpty(track.Artist) && !string.IsNullOrEmpty(track.Name) && string.IsNullOrEmpty(this.ItunesApplication.CurrentStreamTitle))
while (version == StateVersion && i++ < retryCount)
{
if (Globals.SaveAlbumArtwork)
try
{
IITArtworkCollection artworkCollection = track.Artwork;

Expand All @@ -133,14 +141,37 @@ private void App_OnPlayerPlayEvent(object sender)
IITArtwork artwork = artworkCollection[1];

artwork.SaveArtworkToFile(this.DefaultArtworkFilePath);

return;
}
else
{
this.SaveBlankImage();
}
}
catch
{
}

if (i == 0)
{
// Clear the image on the first fail, so that it works if there is no album art at all.
this.SaveBlankImage();
}

await Task.Delay(retryDelay);
}
}

private async void App_OnPlayerPlayEvent(object sender)
{
int currentVersion = ++StateVersion;

IITTrack track = this.ItunesApplication.CurrentTrack;

if (!string.IsNullOrEmpty(track.Artist) && !string.IsNullOrEmpty(track.Name) && string.IsNullOrEmpty(this.ItunesApplication.CurrentStreamTitle))
{
Task updateAlbumTask = Globals.SaveAlbumArtwork ? UpdateAlbumArtAsync(currentVersion, track) : Task.CompletedTask;

TextHandler.UpdateText(track.Name, track.Artist, track.Album);

await updateAlbumTask;
}
else if (string.IsNullOrEmpty(track.Artist) && !string.IsNullOrEmpty(track.Name) && string.IsNullOrEmpty(this.ItunesApplication.CurrentStreamTitle))
{
Expand All @@ -152,29 +183,19 @@ private void App_OnPlayerPlayEvent(object sender)
}
}

private void App_OnPlayerPlayingTrackChangedEvent(object sender)
private async void App_OnPlayerPlayingTrackChangedEvent(object sender)
{
int currentVersion = ++StateVersion;

IITTrack track = this.ItunesApplication.CurrentTrack;

if (!string.IsNullOrEmpty(track.Artist) && !string.IsNullOrEmpty(track.Name) && string.IsNullOrEmpty(this.ItunesApplication.CurrentStreamTitle))
{
if (Globals.SaveAlbumArtwork)
{
try
{
IITArtworkCollection artworkCollection = track.Artwork;
IITArtwork artwork = artworkCollection[1];

artwork.SaveArtworkToFile(this.DefaultArtworkFilePath);
}
catch
{
this.SaveBlankImage();
throw;
}
}
Task updateAlbumTask = Globals.SaveAlbumArtwork ? UpdateAlbumArtAsync(currentVersion, track) : Task.CompletedTask;

TextHandler.UpdateText(track.Name, track.Artist, track.Album);

await updateAlbumTask;
}
else if (!string.IsNullOrEmpty(this.ItunesApplication.CurrentStreamTitle))
{
Expand All @@ -184,6 +205,7 @@ private void App_OnPlayerPlayingTrackChangedEvent(object sender)

private void App_OnPlayerStopEvent(object o)
{
++StateVersion;
if (Globals.SaveAlbumArtwork)
{
this.SaveBlankImage();
Expand All @@ -194,6 +216,8 @@ private void App_OnPlayerStopEvent(object o)

private void App_OnPlayerQuittingEvent()
{
++StateVersion;

if (Globals.SaveAlbumArtwork)
{
this.SaveBlankImage();
Expand Down

0 comments on commit 36a6a45

Please sign in to comment.