Skip to content
New issue

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

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

Already on GitHub? # to your account

Add support for local files and package resources to MediaElement #2502

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

ne0rrmatrix
Copy link
Contributor

Description of Change

Add support for file and resource file types to MediaElement.MetadataArtworkSource. This will allow developers to set artwork using MediaSource class.

Linked Issues

PR Checklist

Additional information

Summary

Add support for local files and package resources as a source for MediaElement.MetadataArtworkUrl. This will add the missing support for all types of files on all device for artwork images.

Motivation

Allow developer more options to add images from more locations to use as artwork for player.

Detailed Design

API Design:

/// <summary>
	/// Backing store for the <see cref="MetadataArtworkUrl"/> property.
	/// </summary>
	public static readonly BindableProperty MetadataArtworkUrlProperty = BindableProperty.Create(nameof(MetadataArtworkSource), typeof(MediaSource), typeof(MediaElement));
/// Gets or sets the Artwork Image Url of the media.
	/// This is a bindable property.
	/// </summary>
	[TypeConverter(typeof(MediaSourceConverter))]
	public MetadataArtworkSource? MetadataArtworkSource
	{
		get => (MediaSource)GetValue(MetadataArtworkUrlProperty);
		set => SetValue(MetadataArtworkUrlProperty, value);
	}

Usage Syntax

XAML:

 <toolkit:MediaElement
     x:Name="MediaElement"
     ShouldAutoPlay="True"
     Source="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
     MetadataArtworkSource="ebedded://robot.jpg"
     MetadataTitle="Big Buck Bunny"
     MetadataArtist="Blender Foundation"/>

Code Behind:

MediaElement.MetadataArtworkSource = MediaSource.FromResource("robot.jpg");  

Updated `PlatformUpdateSource` to be asynchronous, re-implemented `SetPoster` method for robustness, and replaced `MetadataArtworkUrl` with `MetadataArtworkSource` across the project.

- Replaced `MetadataArtworkUrl` with `MetadataArtworkSource` in `MediaElementPage.xaml` and related files.
- Introduced `loadCustomMediaSource` constant and `saveDirectory` string in `MediaElementPage.xaml.cs`.
- Updated `ChangeSourceClicked` method to handle new media source and artwork property.
- Added file handling methods: `Savefile`, `GetFileName`, and `PickAndShow`.
- Updated `IMediaElement` and `MediaElement` class to use `MetadataArtworkSource`.
- Modified `SetMetadata` in `Metadata.macios.cs` for new artwork property.
- Removed `Metadata` class from `Metadata.windows.cs`.
- Enhanced `MediaManager.android.cs` to handle new artwork property and fetch image data.
- Added `BlankByteArray` method and `PlaybackState` class in `MediaManager.android.cs`.
- Made `PlatformUpdateSource` in `MediaManager.macios.cs` asynchronous, updated `Dispose` method.
- Added `GetArtwork` struct for fetching artwork in `MediaManager.macios.cs`.
- Updated `MediaManager.windows.cs` to handle new artwork property and added `ArtworkUrl` method.
- Updated `MediaElementTests` for new artwork property.
Simplified `SetMetadata` in `Metadata.macios.cs` by removing a null check for `artwork` before checking if it is a `UIImage`. Cleaned up `Dispose` in `MediaManager.android.cs` by removing redundant empty lines. Streamlined `StopService` in `MediaManager.android.cs` by removing `HttpClient` usage and `GetBytesFromMetadataArtworkUrl` method. Updated `Dispose` in `MediaManager.macios.cs` to change `SetPoster` return type from `Task` to `ValueTask`. Refactored `UpdateMetadata` in `MediaManager.windows.cs` to handle different `MetadataArtworkSource` types explicitly and removed redundant `ArtworkUrl` method. Cleaned up `OnPlaybackSessionPlaybackStateChanged` in `MediaManager.windows.cs` by removing the now redundant `ArtworkUrl` method.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant