Skip to content

Commit

Permalink
Refactor image handling
Browse files Browse the repository at this point in the history
  • Loading branch information
martijn00 committed Sep 9, 2019
1 parent fb5a57b commit fb5ad6e
Show file tree
Hide file tree
Showing 21 changed files with 218 additions and 406 deletions.
74 changes: 32 additions & 42 deletions MediaManager/Library/IMediaItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,100 +30,90 @@ public interface IMediaItem : IContentItem
/// </summary>
string Album { get; set; }

/// <summary>
/// The metadata key for a Bitmap typed value to retrieve the information about the artwork for the Album of the media's original source.
/// </summary>
object AlbumArt { get; set; }

/// <summary>
/// The metadata for the artist for the Album of the media's original source.
/// </summary>
string AlbumArtist { get; set; }

/// <summary>
/// The metadata key for a CharSequence or string typed value to retrieve the information about the Uri of the artwork for the Album of the media's original source.
/// The metadata for a Bitmap typed value to retrieve the information about the artwork for the Album of the media's original source.
/// </summary>
string AlbumArtUri { get; set; }
object AlbumImage { get; set; }

/// <summary>
/// The metadata key for a Bitmap typed value to retrieve the information about the artwork for the media.
/// The metadata for a CharSequence or string typed value to retrieve the information about the Uri of the artwork for the Album of the media's original source.
/// </summary>
object Art { get; set; }
string AlbumImageUri { get; set; }

/// <summary>
/// The metadata key for a CharSequence or string typed value to retrieve the information about the artist of the media.
/// The metadata for a CharSequence or string typed value to retrieve the information about the artist of the media.
/// </summary>
string Artist { get; set; }

/// <summary>
/// The metadata key for a CharSequence or string typed value to retrieve the information about Uri of the artwork for the media.
/// The metadata for a Bitmap typed value to retrieve the information about the artwork for the media.
/// </summary>
object Image { get; set; }

/// <summary>
/// The metadata for a CharSequence or string typed value to retrieve the information about Uri of the artwork for the media.
/// </summary>
string ArtUri { get; set; }
string ImageUri { get; set; }

/// <summary>
/// The metadata key for a CharSequence or string typed value to retrieve the information about the author of the media.
/// The metadata for a CharSequence or string typed value to retrieve the information about the author of the media.
/// </summary>
string Author { get; set; }

//TODO: Probably remove this one
/// <summary>
/// The metadata key for a int typed value to retrieve the information about the bluetooth folder type of the media specified in the section 6.10.2.2 of the Bluetooth AVRCP 1.5.
/// The metadata for a int typed value to retrieve the information about the bluetooth folder type of the media specified in the section 6.10.2.2 of the Bluetooth AVRCP 1.5.
/// </summary>
BtFolderType BtFolderType { get; set; }

/// <summary>
/// The metadata key for a CharSequence or string typed value to retrieve the information about the compilation status of the media.
/// The metadata for a CharSequence or string typed value to retrieve the information about the compilation status of the media.
/// </summary>
string Compilation { get; set; }

/// <summary>
/// The metadata key for a CharSequence or string typed value to retrieve the information about the composer of the media.
/// The metadata for a CharSequence or string typed value to retrieve the information about the composer of the media.
/// </summary>
string Composer { get; set; }

//TODO: Make it a DateTime
/// <summary>
/// The metadata key for a CharSequence or string typed value to retrieve the information about the date the media was created or published.
/// The metadata for a CharSequence or string typed value to retrieve the information about the date the media was created or published.
/// </summary>
string Date { get; set; }

/// <summary>
/// The metadata key for a int typed value to retrieve the information about the disc number for the media's original source.
/// The metadata for a int typed value to retrieve the information about the disc number for the media's original source.
/// </summary>
int DiscNumber { get; set; }

/// <summary>
/// The metadata key for a CharSequence or string typed value to retrieve the information about the description that is suitable for display to the user.
/// The metadata for a CharSequence or string typed value to retrieve the information about the description that is suitable for display to the user.
/// </summary>
string DisplayDescription { get; set; }

/// <summary>
/// The metadata key for a Bitmap typed value to retrieve the information about the icon or thumbnail that is suitable for display to the user.
/// </summary>
object DisplayIcon { get; set; }

/// <summary>
/// The metadata key for a CharSequence or string typed value to retrieve the information about the Uri of icon or thumbnail that is suitable for display to the user.
/// </summary>
string DisplayIconUri { get; set; }

/// <summary>
/// The metadata key for a CharSequence or string typed value to retrieve the information about the subtitle that is suitable for display to the user.
/// The metadata for a CharSequence or string typed value to retrieve the information about the subtitle that is suitable for display to the user.
/// </summary>
string DisplaySubtitle { get; set; }

/// <summary>
/// The metadata key for a CharSequence or string typed value to retrieve the information about the title that is suitable for display to the user.
/// The metadata for a CharSequence or string typed value to retrieve the information about the title that is suitable for display to the user.
/// </summary>
string DisplayTitle { get; set; }

/// <summary>
/// The metadata key for a int typed value to retrieve the information about the download status of the media which will be used for later offline playback.
/// The metadata for a int typed value to retrieve the information about the download status of the media which will be used for later offline playback.
/// </summary>
DownloadStatus DownloadStatus { get; set; }

/// <summary>
/// The metadata key for a int typed value to retrieve the information about the duration of the media in ms.
/// The metadata for a int typed value to retrieve the information about the duration of the media in ms.
/// </summary>
TimeSpan Duration { get; set; }

Expand All @@ -133,47 +123,47 @@ public interface IMediaItem : IContentItem
object Extras { get; set; }

/// <summary>
/// The metadata key for a CharSequence or string typed value to retrieve the information about the genre of the media.
/// The metadata for a CharSequence or string typed value to retrieve the information about the genre of the media.
/// </summary>
string Genre { get; set; }

/// <summary>
/// The metadata key for a CharSequence or string typed value to retrieve the information about the Uri of the content.
/// The metadata for a CharSequence or string typed value to retrieve the information about the Uri of the content.
/// </summary>
string MediaUri { get; set; }

/// <summary>
/// The metadata key for a int typed value to retrieve the information about the number of tracks in the media's original source.
/// The metadata for a int typed value to retrieve the information about the number of tracks in the media's original source.
/// </summary>
int NumTracks { get; set; }

/// <summary>
/// The metadata key for a Rating2 typed value to retrieve the information about the overall rating for the media.
/// The metadata for a Rating2 typed value to retrieve the information about the overall rating for the media.
/// </summary>
object Rating { get; set; }

/// <summary>
/// The metadata key for a CharSequence or string typed value to retrieve the information about the title of the media.
/// The metadata for a CharSequence or string typed value to retrieve the information about the title of the media.
/// </summary>
string Title { get; set; }

/// <summary>
/// The metadata key for a int typed value to retrieve the information about the track number for the media.
/// The metadata for a int typed value to retrieve the information about the track number for the media.
/// </summary>
int TrackNumber { get; set; }

/// <summary>
/// The metadata key for a Rating2 typed value to retrieve the information about the user's rating for the media.
/// The metadata for a Rating2 typed value to retrieve the information about the user's rating for the media.
/// </summary>
object UserRating { get; set; }

/// <summary>
/// The metadata key for a CharSequence or string typed value to retrieve the information about the writer of the media.
/// The metadata for a CharSequence or string typed value to retrieve the information about the writer of the media.
/// </summary>
string Writer { get; set; }

/// <summary>
/// The metadata key for a int typed value to retrieve the information about the year the media was created or published.
/// The metadata for a int typed value to retrieve the information about the year the media was created or published.
/// </summary>
int Year { get; set; }

Expand Down
8 changes: 4 additions & 4 deletions MediaManager/Library/MediaItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public string Album
}

private object _albumArt;
public object AlbumArt
public object AlbumImage
{
get => _albumArt;
set => SetProperty(ref _albumArt, value);
Expand All @@ -47,14 +47,14 @@ public string AlbumArtist
}

private string _albumArtUri;
public string AlbumArtUri
public string AlbumImageUri
{
get => _albumArtUri;
set => SetProperty(ref _albumArtUri, value);
}

private object _art;
public object Art
public object Image
{
get => _art;
set => SetProperty(ref _art, value);
Expand All @@ -68,7 +68,7 @@ public string Artist
}

private string _artUri;
public string ArtUri
public string ImageUri
{
get => _artUri;
set => SetProperty(ref _artUri, value);
Expand Down
20 changes: 20 additions & 0 deletions MediaManager/Library/MediaItemExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,25 @@ public static string GetSubText(this IMediaItem mediaItem)
else
return "";
}

public static string GetImageUri(this IMediaItem mediaItem)
{
if (!string.IsNullOrEmpty(mediaItem.ImageUri))
return mediaItem.ImageUri;
else if (!string.IsNullOrEmpty(mediaItem.AlbumImageUri))
return mediaItem.AlbumImageUri;
else
return "";
}

public static object GetImage(this IMediaItem mediaItem)
{
if (mediaItem.Image != null)
return mediaItem.Image;
else if (mediaItem.AlbumImage != null)
return mediaItem.AlbumImage;
else
return null;
}
}
}
16 changes: 12 additions & 4 deletions MediaManager/Media/MediaExtractorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public virtual async Task<IMediaItem> UpdateMediaItem(IMediaItem mediaItem)
}

mediaItem = await GetMetadata(mediaItem).ConfigureAwait(false);
mediaItem.Image = await GetMediaImage(mediaItem).ConfigureAwait(false);
mediaItem.IsMetadataExtracted = true;
}

Expand All @@ -163,11 +164,18 @@ public async Task<IMediaItem> GetMetadata(IMediaItem mediaItem)
public async Task<object> GetMediaImage(IMediaItem mediaItem)
{
object image = null;
foreach (var provider in ImageProviders)

if (mediaItem.IsMetadataExtracted)
image = mediaItem.GetImage();

if (image == null)
{
image = await provider.ProvideImage(mediaItem).ConfigureAwait(false);
if (image != null)
return image;
foreach (var provider in ImageProviders)
{
image = await provider.ProvideImage(mediaItem).ConfigureAwait(false);
if (image != null)
return image;
}
}
return image;
}
Expand Down
2 changes: 2 additions & 0 deletions MediaManager/MediaManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@

<ItemGroup Condition=" $(TargetFramework.StartsWith('xamarin.tvos')) ">
<None Remove="Platforms\Apple\**\*.cs" />
<None Remove="Platforms\Ios\**\*.cs" />
<None Remove="Platforms\Tvos\**\*.cs" />
<None Remove="Platforms\Xamarin\**\*.cs" />
<Compile Include="Platforms\Apple\**\*.cs" />
<Compile Include="Platforms\Ios\**\*.cs" />
<Compile Include="Platforms\Tvos\**\*.cs" />
<Compile Include="Platforms\Xamarin\**\*.cs" />
</ItemGroup>
Expand Down
44 changes: 26 additions & 18 deletions MediaManager/Platforms/Android/Media/FileImageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,40 @@ namespace MediaManager.Platforms.Android.Media
{
public class FileImageProvider : IMediaItemImageProvider
{
public Task<object> ProvideImage(IMediaItem mediaItem)
public async Task<object> ProvideImage(IMediaItem mediaItem)
{
var albumFolder = GetCurrentSongFolder(mediaItem);
if (albumFolder == null)
return null;

if (!albumFolder.EndsWith("/"))
object image = null;
try
{
albumFolder += "/";
}
var albumFolder = GetCurrentSongFolder(mediaItem);
if (albumFolder == null)
return null;

var baseUri = new System.Uri(albumFolder);
var albumArtPath = TryGetAlbumArtPathByFilename(baseUri, "Folder.jpg");
if (albumArtPath == null)
{
albumArtPath = TryGetAlbumArtPathByFilename(baseUri, "Cover.jpg");
if (!albumFolder.EndsWith("/"))
{
albumFolder += "/";
}

var baseUri = new System.Uri(albumFolder);
var albumArtPath = TryGetAlbumArtPathByFilename(baseUri, "Folder.jpg");
if (albumArtPath == null)
{
albumArtPath = TryGetAlbumArtPathByFilename(baseUri, "AlbumArtSmall.jpg");
albumArtPath = TryGetAlbumArtPathByFilename(baseUri, "Cover.jpg");
if (albumArtPath == null)
return null;
{
albumArtPath = TryGetAlbumArtPathByFilename(baseUri, "AlbumArtSmall.jpg");
if (albumArtPath == null)
return null;
}
}
}

var bitmap = BitmapFactory.DecodeFile(albumArtPath);
return Task.FromResult(bitmap as object);
image = await BitmapFactory.DecodeFileAsync(albumArtPath).ConfigureAwait(false);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return mediaItem.AlbumImage = image;
}

protected virtual string TryGetAlbumArtPathByFilename(System.Uri baseUri, string filename)
Expand Down
4 changes: 2 additions & 2 deletions MediaManager/Platforms/Android/Media/ID3Provider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public async Task<object> ProvideImage(IMediaItem mediaItem)
{
try
{
image = await BitmapFactory.DecodeByteArrayAsync(imageByteArray, 0, imageByteArray.Length);
image = await BitmapFactory.DecodeByteArrayAsync(imageByteArray, 0, imageByteArray.Length).ConfigureAwait(false);
}
catch (Java.Lang.OutOfMemoryError)
{
Expand All @@ -119,7 +119,7 @@ public async Task<object> ProvideImage(IMediaItem mediaItem)
{
Console.WriteLine(ex.Message);
}
return image;
return mediaItem.Image = image;
}

public async Task<object> ProvideVideoFrame(IMediaItem mediaItem, TimeSpan timeFromStart)
Expand Down
12 changes: 11 additions & 1 deletion MediaManager/Platforms/Android/Media/MediaDescriptionAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Android.App;
using Android.Graphics;
using Android.Runtime;
Expand Down Expand Up @@ -38,7 +39,16 @@ public string GetCurrentContentTitle(IPlayer player)

public Bitmap GetCurrentLargeIcon(IPlayer player, PlayerNotificationManager.BitmapCallback callback)
{
return MediaManager.Queue.ElementAtOrDefault(player.CurrentWindowIndex)?.GetCover();
var mediaItem = MediaManager.Queue.ElementAtOrDefault(player.CurrentWindowIndex);
if (mediaItem != null)
{
Task.Run(async () =>
{
var image = await MediaManager.Extractor.GetMediaImage(mediaItem).ConfigureAwait(false) as Bitmap;
callback.OnBitmap(image);
}).ConfigureAwait(false);
}
return null;
}

public string GetCurrentSubText(IPlayer player)
Expand Down
Loading

0 comments on commit fb5ad6e

Please sign in to comment.