unoplatform/uno · error · InvalidOperationException

MediaPlayerPresenterExtension must be initialized with a Med

Error message

MediaPlayerPresenterExtension must be initialized with a MediaPlayer instance

What it means

InvalidOperationException thrown by the MediaPlayerExtension constructor (WebAssembly) when the 'owner' argument is not a MediaPlayer instance. The add-in is registered via ApiExtension to implement IMediaPlayerExtension and is constructed with the owning object; the framework contract requires that owner to be a MediaPlayer. A non-MediaPlayer owner indicates a misconfigured extensibility registration or manual misuse.

Source

Thrown at src/AddIns/Uno.UI.MediaPlayer.WebAssembly/MediaPlayerExtension.cs:52

	private bool _isLoopingEnabled;
	private bool _isLoopingAllEnabled;
	private List<Uri>? _playlistItems;
	private int _playlistIndex;
	private TimeSpan _naturalDuration;
	private Uri? _uri;
	private bool _anonymousCors = FeatureConfiguration.AnonymousCorsDefault;

	const string MsAppXScheme = "ms-appx";

	public MediaPlayerExtension(object owner)
	{
		if (owner is MediaPlayer player)
		{
			_owner = player;
		}
		else
		{
			throw new InvalidOperationException($"MediaPlayerPresenterExtension must be initialized with a MediaPlayer instance");
		}

		lock (_instances)
		{
			_instances[_owner] = this;
		}
	}

	~MediaPlayerExtension()
	{
		lock (_instances)
		{
			_instances.Remove(_owner);
		}
	}

	internal static MediaPlayerExtension? GetByMediaPlayer(MediaPlayer mediaPlayer)
	{

View on GitHub (pinned to 0418340488)

Solutions

  1. Ensure Uno.UI.MediaPlayer.WebAssembly package version matches the Uno.UI version in use.
  2. Do not construct MediaPlayerExtension directly; let the framework's ApiExtension resolution create it with the MediaPlayer owner.
  3. If wiring manually, pass the MediaPlayer instance, not the MediaPlayerPresenter/element.
  4. Verify [assembly: ApiExtension(...)] registration targets the correct owner type.

Example fix

// before — wrong owner type
var ext = new MediaPlayerExtension(mediaPlayerPresenter);

// after — correct owner
var ext = new MediaPlayerExtension(mediaPlayer);
Defensive patterns

Strategy: type-guard

Validate before calling

// Only construct with a MediaPlayer owner
if (owner is MediaPlayer mp)
    _ext = new MediaPlayerExtension(mp);
else
    throw new ArgumentException("owner must be a MediaPlayer", nameof(owner));

Type guard

bool IsMediaPlayerOwner(object owner) => owner is MediaPlayer;

Prevention

When it happens

Trigger: Manually new-ing MediaPlayerExtension with a non-MediaPlayer object; a wrong ApiExtension registration wiring it to a different type; the framework passing the wrong owner due to an add-in version mismatch between Uno.UI and the WebAssembly media extension.

Common situations: Mixing incompatible Uno.UI and Uno.UI.MediaPlayer.WebAssembly package versions; a custom IMediaPlayerExtension registration that instantiates with the presenter instead of the player; reflection-based construction passing the wrong object.

Related errors


AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13). Data as JSON: /api/errors/93381f6872df9633. Report an issue: GitHub.