Skip to content

Conversation

@ne0rrmatrix
Copy link
Member

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

  • Has a linked Issue, and the Issue has been approved(bug) or Championed (feature/proposal)
  • Has tests (if omitted, state reason in description)
  • Has samples (if omitted, state reason in description)
  • Rebased on top of main at time of PR
  • Changes adhere to coding standard
  • Documentation created or updated: https://github.com/MicrosoftDocs/CommunityToolkit/pulls

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.
@dotnet-policy-service dotnet-policy-service bot added stale The author has not responded in over 30 days help wanted This proposal has been approved and is ready to be implemented labels May 1, 2025
Added "Load from File" option to MediaElementPage for local file playback. Refactored metadata artwork handling for Android and Windows to support file, resource, and URI sources. Unified MediaItem metadata creation on Android and improved artwork path resolution on Windows. Removed obsolete Metadata.windows.cs and integrated its logic. Includes minor bug fixes and code cleanups.
Refactored GetSource in MediaManager.macios.cs to use a switch statement with pattern matching for improved readability and maintainability. Removed the unused LoadBitmapImageAsync helper from MediaManager.windows.cs.
Copilot AI review requested due to automatic review settings January 30, 2026 04:24
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 12 changed files in this pull request and generated 16 comments.

Introduce isAndroidForegroundServiceEnabled to track service state. Update CreatePlatformView to accept and set this flag. Ensure notifications and service startup only occur when the foreground service is enabled, preventing unnecessary operations.
Removed unnecessary trace message in UpdateNotifications; now the method returns silently if the notification service is not running. This reduces log noise and streamlines error handling.
Copilot AI review requested due to automatic review settings January 30, 2026 04:38
Refactored PlatformUpdateSource to call MediaElement.MediaOpened()
only when there is no PlayerError, and to update notifications
only when the Android foreground service is enabled. This improves
clarity and ensures notifications are updated independently of
media errors.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 12 changed files in this pull request and generated 7 comments.

Refactored file and memory stream usage to use 'using' statements for proper disposal. Replaced synchronous CopyTo with asynchronous CopyToAsync for non-blocking I/O. Removed redundant byte array conversions by returning the result directly.
- Change GetSource to internal and remove XML docs.
- Wrap ResourceMediaSource file access in try-catch; log warning if not found.
- Only set thumbnail/poster if stream and uri are valid.
- Always update system media controls metadata.
- Clean up and reorganize redundant updater code.
Copilot AI review requested due to automatic review settings January 30, 2026 05:20
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 12 changed files in this pull request and generated 13 comments.

Copilot AI review requested due to automatic review settings January 30, 2026 05:28
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 12 changed files in this pull request and generated 9 comments.

- Add foreground service enabled check to Android StopService
- Make GetMauiAssetBytes cancellable via CancellationToken
- Log and handle missing/empty sources in Windows media update
- Always set DisplayUpdater.Type to Music in Windows metadata
GetArtworkFromMediasource and its helper methods now accept an optional CancellationToken parameter, enabling cancellation of asynchronous operations for improved responsiveness and resource management.
Copilot AI review requested due to automatic review settings January 30, 2026 05:57
ne0rrmatrix and others added 2 commits January 29, 2026 21:58
Renamed the MetadataArtworkUrl method and all its references to MetadataArtworkMediaSource to better reflect that it operates on a MediaSource, not just a URL. This improves code clarity and intent.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 12 changed files in this pull request and generated 4 comments.

Refactored MetadataArtworkMediaSource to use a switch statement for improved readability and maintainability. Updated CreateMediaItem in MediaManager.android.cs to accept a CancellationToken, enabling cancellation during asynchronous artwork retrieval.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

help wanted This proposal has been approved and is ready to be implemented stale The author has not responded in over 30 days

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant