From af8f126ea968f9415163db4432109c2e2906e703 Mon Sep 17 00:00:00 2001 From: Martijn van Dijk Date: Thu, 17 Oct 2019 17:09:04 +0200 Subject: [PATCH] Extractor fixes --- Directory.build.props | 2 +- MediaManager/Library/DownloadStatus.cs | 7 +- MediaManager/Library/IMediaItem.cs | 9 ++ MediaManager/Library/MediaItem.cs | 102 ++++++++++++++---- MediaManager/Library/MediaItemExtensions.cs | 26 +---- MediaManager/Media/MediaExtractorBase.cs | 61 ++++++++--- .../Android/Media/MediaDescriptionAdapter.cs | 19 ++-- .../Android/Media/MediaItemExtensions.cs | 18 ++-- .../ElementPlayer.Core/MediaItemProvider.cs | 6 +- .../ViewModels/MiniPlayerViewModel.cs | 4 +- .../ViewModels/PlayerViewModel.cs | 4 +- 11 files changed, 172 insertions(+), 86 deletions(-) diff --git a/Directory.build.props b/Directory.build.props index d21ae02e..e2ec9961 100644 --- a/Directory.build.props +++ b/Directory.build.props @@ -15,7 +15,7 @@ git $(AssemblyName) ($(TargetFramework)) en - 0.8.16 + 0.8.17 AnyCPU 7.3 diff --git a/MediaManager/Library/DownloadStatus.cs b/MediaManager/Library/DownloadStatus.cs index 4694d5a2..61004e47 100644 --- a/MediaManager/Library/DownloadStatus.cs +++ b/MediaManager/Library/DownloadStatus.cs @@ -2,8 +2,9 @@ { public enum DownloadStatus { - NotDownloaded = 0, - Downloading, - Downloaded + Unknown = 0, + NotDownloaded = 1, + Downloading = 2, + Downloaded = 3 } } diff --git a/MediaManager/Library/IMediaItem.cs b/MediaManager/Library/IMediaItem.cs index f96183e3..9f3c46a9 100644 --- a/MediaManager/Library/IMediaItem.cs +++ b/MediaManager/Library/IMediaItem.cs @@ -85,6 +85,10 @@ public interface IMediaItem : IContentItem /// int DiscNumber { get; set; } + object DisplayImage { get; set; } + + string DisplayImageUri { get; set; } + /// /// The metadata for a CharSequence or string typed value to retrieve the information about the description that is suitable for display to the user. /// @@ -166,6 +170,11 @@ public interface IMediaItem : IContentItem /// string FileExtension { get; set; } + /// + /// The name of the media file + /// + string FileName { get; set; } + /// /// The type of the media item /// Standard Type is Default which will try to play in the standard way. diff --git a/MediaManager/Library/MediaItem.cs b/MediaManager/Library/MediaItem.cs index c9a02187..1602033a 100644 --- a/MediaManager/Library/MediaItem.cs +++ b/MediaManager/Library/MediaItem.cs @@ -109,42 +109,101 @@ public int DiscNumber set => SetProperty(ref _discNumber, value); } - private string _displayDescription; - public string DisplayDescription + private object _displayImage; + public object DisplayImage { - get => _displayDescription; - set => SetProperty(ref _displayDescription, value); + get + { + if (_displayImage != null) + return _displayImage; + if (Image != null) + return Image; + else if (AlbumImage != null) + return AlbumImage; + else + return null; + } + + set => SetProperty(ref _displayImage, value); } - private object _displayIcon; - public object DisplayIcon + private string _displayImageUri; + public string DisplayImageUri { - get => _displayIcon; - set => SetProperty(ref _displayIcon, value); + get + { + if (!string.IsNullOrEmpty(_displayImageUri)) + return _displayImageUri; + if (!string.IsNullOrEmpty(ImageUri)) + return ImageUri; + else if (!string.IsNullOrEmpty(AlbumImageUri)) + return AlbumImageUri; + else + return string.Empty; + } + + set => SetProperty(ref _displayImageUri, value); } - private string _displayIconUri; - public string DisplayIconUri + private string _displayTitle; + public string DisplayTitle { - get => _displayIconUri; - set => SetProperty(ref _displayIconUri, value); + get + { + if (!string.IsNullOrEmpty(_displayTitle)) + return _displayTitle; + else if (!string.IsNullOrEmpty(Title)) + return Title; + else if (!string.IsNullOrEmpty(FileName)) + return FileName; + else + return string.Empty; + } + + set => SetProperty(ref _displayTitle, value); } private string _displaySubtitle; public string DisplaySubtitle { - get => _displaySubtitle; + get + { + if (!string.IsNullOrEmpty(_displaySubtitle)) + return _displaySubtitle; + else if (!string.IsNullOrEmpty(Artist)) + return Artist; + else if (!string.IsNullOrEmpty(AlbumArtist)) + return AlbumArtist; + else if (!string.IsNullOrEmpty(Album)) + return Album; + else + return string.Empty; + } + set => SetProperty(ref _displaySubtitle, value); } - private string _displayTitle; - public string DisplayTitle + private string _displayDescription; + public string DisplayDescription { - get => _displayTitle; - set => SetProperty(ref _displayTitle, value); + get + { + if (!string.IsNullOrEmpty(_displayDescription)) + return _displayDescription; + else if (!string.IsNullOrEmpty(Album)) + return Album; + else if (!string.IsNullOrEmpty(Artist)) + return Artist; + else if (!string.IsNullOrEmpty(AlbumArtist)) + return AlbumArtist; + else + return string.Empty; + } + + set => SetProperty(ref _displayDescription, value); } - private DownloadStatus _downloadStatus = DownloadStatus.NotDownloaded; + private DownloadStatus _downloadStatus = DownloadStatus.Unknown; public DownloadStatus DownloadStatus { get => _downloadStatus; @@ -235,6 +294,13 @@ public string FileExtension set => SetProperty(ref _fileExtension, value); } + private string _fileName; + public string FileName + { + get => _fileName; + set => SetProperty(ref _fileName, value); + } + private MediaType _mediaType = MediaType.Default; public MediaType MediaType { diff --git a/MediaManager/Library/MediaItemExtensions.cs b/MediaManager/Library/MediaItemExtensions.cs index 90e39def..ded33bd5 100644 --- a/MediaManager/Library/MediaItemExtensions.cs +++ b/MediaManager/Library/MediaItemExtensions.cs @@ -23,7 +23,7 @@ public static string GetTitle(this IMediaItem mediaItem) else if (!string.IsNullOrEmpty(mediaItem.Title)) return mediaItem.Title; else - return ""; + return string.Empty; } public static string GetContentTitle(this IMediaItem mediaItem) @@ -37,7 +37,7 @@ public static string GetContentTitle(this IMediaItem mediaItem) else if (!string.IsNullOrEmpty(mediaItem.Album)) return mediaItem.Album; else - return ""; + return string.Empty; } public static string GetSubText(this IMediaItem mediaItem) @@ -49,27 +49,7 @@ public static string GetSubText(this IMediaItem mediaItem) else if (!string.IsNullOrEmpty(mediaItem.AlbumArtist)) return mediaItem.AlbumArtist; 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; + return string.Empty; } } } diff --git a/MediaManager/Media/MediaExtractorBase.cs b/MediaManager/Media/MediaExtractorBase.cs index 3bf43632..fa9a7e1a 100644 --- a/MediaManager/Media/MediaExtractorBase.cs +++ b/MediaManager/Media/MediaExtractorBase.cs @@ -132,22 +132,29 @@ public virtual async Task CreateMediaItemFromResource(string resourc public virtual async Task UpdateMediaItem(IMediaItem mediaItem) { - if (!mediaItem.IsMetadataExtracted) + if (string.IsNullOrEmpty(mediaItem.FileName)) + { + mediaItem.FileName = GetFileName(mediaItem); + } + if (string.IsNullOrEmpty(mediaItem.FileExtension)) + { + mediaItem.FileExtension = GetFileExtension(mediaItem); + } + if (mediaItem.MediaLocation == MediaLocation.Unknown) + { + mediaItem.MediaLocation = GetMediaLocation(mediaItem); + } + if (mediaItem.MediaType == MediaType.Default) + { + mediaItem.MediaType = GetMediaType(mediaItem); + } + if (mediaItem.DownloadStatus == DownloadStatus.Unknown) { - if (string.IsNullOrEmpty(mediaItem.FileExtension)) - { - mediaItem.FileExtension = GetFileExtension(mediaItem); - } - if (mediaItem.MediaLocation == MediaLocation.Unknown) - { - mediaItem.MediaLocation = GetMediaLocation(mediaItem); - } - if (mediaItem.MediaType == MediaType.Default) - { - mediaItem.MediaType = GetMediaType(mediaItem); - } mediaItem.DownloadStatus = GetDownloadStatus(mediaItem); + } + if (!mediaItem.IsMetadataExtracted) + { mediaItem = await GetMetadata(mediaItem).ConfigureAwait(false); mediaItem.Image = await GetMediaImage(mediaItem).ConfigureAwait(false); mediaItem.IsMetadataExtracted = true; @@ -172,7 +179,7 @@ public async Task GetMediaImage(IMediaItem mediaItem) object image = null; if (mediaItem.IsMetadataExtracted) - image = mediaItem.GetImage(); + image = mediaItem.DisplayImage; if (image == null) { @@ -200,21 +207,42 @@ public async Task GetVideoFrame(IMediaItem mediaItem, TimeSpan timeFromS protected abstract Task GetResourcePath(string resourceName); + public virtual string GetFileName(IMediaItem mediaItem) + { + if (string.IsNullOrEmpty(mediaItem?.MediaUri)) + return string.Empty; + + string url; + if (!Uri.TryCreate(mediaItem.MediaUri, UriKind.Absolute, out var uri)) + uri = new Uri(mediaItem.MediaUri); + + url = Path.GetFileName(uri.LocalPath); + if (string.IsNullOrEmpty(url)) + url = uri.Segments.LastOrDefault(); + + return url; + } + public virtual string GetFileExtension(IMediaItem mediaItem) { var url = mediaItem?.MediaUri?.ToLower(); + var fileName = mediaItem?.FileName; + + if (string.IsNullOrEmpty(url) && string.IsNullOrEmpty(fileName)) + return string.Empty; + var suffixes = VideoSuffixes.Union(AudioSuffixes).Union(HlsSuffixes).Union(DashSuffixes).Union(SmoothStreamingSuffixes); //Try to find the best match foreach (var item in suffixes) { - if (url.EndsWith(item)) + if (fileName.EndsWith(item) || url.EndsWith(item)) return item; } //If no match available see if the url contains info foreach (var item in suffixes) { - if (url.Contains(item)) + if (fileName.Contains(item) || url.Contains(item)) return item; } return mediaItem?.FileExtension; @@ -278,6 +306,7 @@ public virtual DownloadStatus GetDownloadStatus(IMediaItem mediaItem) switch (mediaItem.MediaLocation) { case MediaLocation.Unknown: + return DownloadStatus.Unknown; case MediaLocation.Remote: return DownloadStatus.NotDownloaded; case MediaLocation.FileSystem: diff --git a/MediaManager/Platforms/Android/Media/MediaDescriptionAdapter.cs b/MediaManager/Platforms/Android/Media/MediaDescriptionAdapter.cs index adc0f939..bf329c6f 100644 --- a/MediaManager/Platforms/Android/Media/MediaDescriptionAdapter.cs +++ b/MediaManager/Platforms/Android/Media/MediaDescriptionAdapter.cs @@ -6,7 +6,6 @@ using Android.Runtime; using Com.Google.Android.Exoplayer2; using Com.Google.Android.Exoplayer2.UI; -using MediaManager.Library; namespace MediaManager.Platforms.Android.Media { @@ -29,18 +28,23 @@ public PendingIntent CreateCurrentContentIntent(IPlayer player) public string GetCurrentContentText(IPlayer player) { - return MediaManager.Queue.ElementAtOrDefault(player.CurrentWindowIndex)?.GetTitle(); + return MediaManager.Queue.ElementAtOrDefault(player.CurrentWindowIndex)?.DisplayTitle; } public string GetCurrentContentTitle(IPlayer player) { - return MediaManager.Queue.ElementAtOrDefault(player.CurrentWindowIndex)?.GetContentTitle(); + return MediaManager.Queue.ElementAtOrDefault(player.CurrentWindowIndex)?.DisplaySubtitle; + } + + public string GetCurrentSubText(IPlayer player) + { + return MediaManager.Queue.ElementAtOrDefault(player.CurrentWindowIndex)?.DisplayDescription; } public Bitmap GetCurrentLargeIcon(IPlayer player, PlayerNotificationManager.BitmapCallback callback) { var mediaItem = MediaManager.Queue.ElementAtOrDefault(player.CurrentWindowIndex); - if (mediaItem != null) + if (mediaItem != null && mediaItem.DisplayImage == null) { Task.Run(async () => { @@ -48,12 +52,7 @@ public Bitmap GetCurrentLargeIcon(IPlayer player, PlayerNotificationManager.Bitm callback.OnBitmap(image); }).ConfigureAwait(false); } - return null; - } - - public string GetCurrentSubText(IPlayer player) - { - return MediaManager.Queue.ElementAtOrDefault(player.CurrentWindowIndex)?.GetSubText(); + return mediaItem?.DisplayImage as Bitmap; } } } diff --git a/MediaManager/Platforms/Android/Media/MediaItemExtensions.cs b/MediaManager/Platforms/Android/Media/MediaItemExtensions.cs index 5d526735..6ee2047f 100644 --- a/MediaManager/Platforms/Android/Media/MediaItemExtensions.cs +++ b/MediaManager/Platforms/Android/Media/MediaItemExtensions.cs @@ -89,17 +89,17 @@ public static MediaDescriptionCompat ToMediaDescription(this IMediaItem item) var description = new MediaDescriptionCompat.Builder() .SetMediaId(item?.Id) .SetMediaUri(global::Android.Net.Uri.Parse(item?.MediaUri)) - .SetTitle(item?.GetTitle()) - .SetSubtitle(item?.GetContentTitle()) + .SetTitle(item?.DisplayTitle) + .SetSubtitle(item?.DisplaySubtitle) .SetDescription(item?.DisplayDescription) .SetExtras(item?.Extras as Bundle); //It should be better to only set the uri to prevent loading images into memory - if (!string.IsNullOrEmpty(item?.GetImageUri())) - description.SetIconUri(global::Android.Net.Uri.Parse(item?.GetImageUri())); + if (!string.IsNullOrEmpty(item?.DisplayImageUri)) + description.SetIconUri(global::Android.Net.Uri.Parse(item?.DisplayImageUri)); else { - var image = item?.GetImage() as Bitmap; + var image = item?.DisplayImage as Bitmap; if (image != null) description.SetIconBitmap(image); } @@ -130,8 +130,8 @@ public static IMediaItem ToMediaItem(this MediaDescriptionCompat mediaDescriptio //item.Date = mediaDescription. //item.DiscNumber = mediaDescription. //item.DisplayDescription = mediaDescription. - item.DisplayIcon = mediaDescription.IconBitmap; - item.DisplayIconUri = mediaDescription.IconUri.ToString(); + item.DisplayImage = mediaDescription.IconBitmap; + item.DisplayImageUri = mediaDescription.IconUri.ToString(); item.DisplaySubtitle = mediaDescription.Subtitle; item.DisplayTitle = mediaDescription.Title; //item.DownloadStatus = mediaDescription. @@ -176,8 +176,8 @@ public static IMediaItem ToMediaItem(this MediaMetadataCompat mediaMetadata) item.DiscNumber = Convert.ToInt32(mediaMetadata.GetLong(MediaMetadataCompat.MetadataKeyDiscNumber)); item.DisplayDescription = mediaMetadata.GetString(MediaMetadataCompat.MetadataKeyDisplayDescription); - item.DisplayIcon = mediaMetadata.GetBitmap(MediaMetadataCompat.MetadataKeyDisplayIcon); - item.DisplayIconUri = mediaMetadata.GetString(MediaMetadataCompat.MetadataKeyDisplayIconUri); + item.DisplayImage = mediaMetadata.GetBitmap(MediaMetadataCompat.MetadataKeyDisplayIcon); + item.DisplayImageUri = mediaMetadata.GetString(MediaMetadataCompat.MetadataKeyDisplayIconUri); item.DisplaySubtitle = mediaMetadata.GetString(MediaMetadataCompat.MetadataKeyDisplaySubtitle); item.DisplayTitle = mediaMetadata.GetString(MediaMetadataCompat.MetadataKeyDisplayTitle); item.DownloadStatus = mediaMetadata.GetLong(MediaMetadataCompat.MetadataKeyDownloadStatus) == 0 ? DownloadStatus.NotDownloaded : DownloadStatus.Downloaded; diff --git a/Samples/ElementPlayer.Core/MediaItemProvider.cs b/Samples/ElementPlayer.Core/MediaItemProvider.cs index 95e893d3..47e8b14b 100644 --- a/Samples/ElementPlayer.Core/MediaItemProvider.cs +++ b/Samples/ElementPlayer.Core/MediaItemProvider.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using ElementPlayer.Core.Assets; +using MediaManager; using MediaManager.Library; using MediaManager.Media; @@ -37,8 +38,9 @@ public async Task> GetAll() { if (!string.IsNullOrEmpty(sample.Uri)) { - //var mediaItem = await CrossMediaManager.Current.Extractor.CreateMediaItem(sample.Uri).ConfigureAwait(false); - var mediaItem = new MediaItem(sample.Uri); + IMediaItem mediaItem = new MediaItem(sample.Uri) { IsMetadataExtracted = true }; + mediaItem = await CrossMediaManager.Current.Extractor.UpdateMediaItem(mediaItem).ConfigureAwait(false); + //var mediaItem = new MediaItem(sample.Uri); mediaItem.Title = sample.Name; mediaItem.Album = item.Name; mediaItem.FileExtension = sample.Extension; diff --git a/Samples/ElementPlayer.Core/ViewModels/MiniPlayerViewModel.cs b/Samples/ElementPlayer.Core/ViewModels/MiniPlayerViewModel.cs index 1fc6e716..d07298fb 100644 --- a/Samples/ElementPlayer.Core/ViewModels/MiniPlayerViewModel.cs +++ b/Samples/ElementPlayer.Core/ViewModels/MiniPlayerViewModel.cs @@ -28,8 +28,8 @@ public MiniPlayerViewModel(IMvxLogProvider logProvider, IMvxNavigationService na public IMediaItem Current => MediaManager.Queue.Current; - public string CurrentTitle => Current.GetTitle(); - public string CurrentSubtitle => Current.GetContentTitle(); + public string CurrentTitle => Current.DisplayTitle; + public string CurrentSubtitle => Current.DisplaySubtitle; public int Buffered => Convert.ToInt32(MediaManager.Buffered.TotalSeconds); public int Duration => Convert.ToInt32(MediaManager.Duration.TotalSeconds); diff --git a/Samples/ElementPlayer.Core/ViewModels/PlayerViewModel.cs b/Samples/ElementPlayer.Core/ViewModels/PlayerViewModel.cs index a8bbefd9..c16b18a3 100644 --- a/Samples/ElementPlayer.Core/ViewModels/PlayerViewModel.cs +++ b/Samples/ElementPlayer.Core/ViewModels/PlayerViewModel.cs @@ -63,8 +63,8 @@ public override async Task Initialize() //Image = image.ToImageSource(); } - public string CurrentTitle => Current.GetTitle(); - public string CurrentSubtitle => Current.GetContentTitle(); + public string CurrentTitle => Current.DisplayTitle; + public string CurrentSubtitle => Current.DisplaySubtitle; public int Buffered => Convert.ToInt32(MediaManager.Buffered.TotalSeconds); public int Duration => Convert.ToInt32(MediaManager.Duration.TotalSeconds);