Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry iTunes album art retrieval #478

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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