Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Toolkit/Toolkit.Maui/AppHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class AppHostBuilderExtensions
/// <returns>The host builder</returns>
public static MauiAppBuilder UseArcGISToolkit(this MauiAppBuilder builder)
{
#if WINDOWS || __IOS__
#if WINDOWS || __IOS__ || ANDROID
builder.ConfigureMauiHandlers(handler => handler.AddHandler<MauiMediaElement, MauiMediaElementHandler>());
#endif
builder.ConfigureFonts(fonts => fonts
Expand Down
108 changes: 108 additions & 0 deletions src/Toolkit/Toolkit.Maui/Internal/MauiMediaElementHandler.Android.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#if ANDROID
using Android.Media;
using Android.Widget;
using AndroidX.CoordinatorLayout.Widget;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Handlers;

namespace Esri.ArcGISRuntime.Toolkit.Maui.Internal;

internal partial class MauiMediaElementHandler : ViewHandler<MauiMediaElement, MauiVideoPlayer>
{
protected override MauiVideoPlayer CreatePlatformView() => new(Context, VirtualView);

protected override void ConnectHandler(MauiVideoPlayer platformView)
{
base.ConnectHandler(platformView);
platformView.UpdateSource(VirtualView.Source);
}

protected override void DisconnectHandler(MauiVideoPlayer platformView)
{
platformView.Dispose();
base.DisconnectHandler(platformView);
}

public void UpdateSource(Uri? source)
{
PlatformView?.UpdateSource(source);
}
}

internal sealed class MauiVideoPlayer : CoordinatorLayout
{
private readonly RelativeLayout _relativeLayout;
private readonly VideoView _videoView;
private readonly MediaController _mediaController;

public MauiVideoPlayer(Android.Content.Context context, MauiMediaElement mediaElement) : base(context)
{
SetBackgroundColor(Android.Graphics.Color.Black);

_relativeLayout = new RelativeLayout(context)
{
LayoutParameters = new CoordinatorLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent)
{
Gravity = (int)Android.Views.GravityFlags.Center,
},
};

_videoView = new VideoView(context)
Copy link
Copy Markdown
Collaborator

@mstefarov mstefarov Apr 28, 2026

Choose a reason for hiding this comment

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

Have you tried this with audio?

MauiMediaElement is created for both MarkupType.Audio and MarkupType.Video. iOS uses AVPlayer and Windows uses MediaPlayerElement; both handle audio fine. I am not sure if Android's VideoView does.

{
LayoutParameters = new RelativeLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent),
};

_relativeLayout.AddView(_videoView);
AddView(_relativeLayout);

_mediaController = new MediaController(context);
_mediaController.SetAnchorView(_videoView);
_mediaController.SetMediaPlayer(_videoView);
_videoView.SetMediaController(_mediaController);
}

protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
var density = Context?.Resources?.DisplayMetrics?.Density ?? 1f;
var heightInPixels = (int)(180 * density);
var exactHeight = Android.Views.View.MeasureSpec.MakeMeasureSpec(heightInPixels, Android.Views.MeasureSpecMode.Exactly);

_relativeLayout.Measure(widthMeasureSpec, exactHeight);
SetMeasuredDimension(Android.Views.View.MeasureSpec.GetSize(widthMeasureSpec), Android.Views.View.MeasureSpec.GetSize(exactHeight));
}

protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
_relativeLayout.Layout(0, 0, r - l, b - t);
}

public void UpdateSource(Uri? source)
{
if (source is null)
{
_videoView.StopPlayback();
return;
}

_videoView.SetVideoURI(Android.Net.Uri.Parse(source.ToString()));
_videoView.RequestFocus();
_videoView.Start();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This means the video always auto-plays, right? We probably shouldn't do that, in case a popup has multiple videos. Our iOS and Windows players require a click to play.

}

protected override void Dispose(bool disposing)
{
if (disposing)
{
_videoView.SetMediaController(null);
_videoView.StopPlayback();
_mediaController.SetMediaPlayer(null);
_mediaController.Dispose();
_videoView.Dispose();
_relativeLayout.Dispose();
}

base.Dispose(disposing);
}
}

#endif
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if WINDOWS || __IOS__
#if WINDOWS || __IOS__ || ANDROID
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The code that creates MauiMediaElement in HtmlToView still excludes Android. Looks like you had this change in an earlier commit (047111de#diff-afafce18e1ef9ab1fae6bd73694a1a77532312212518ab8f140de36224420da7) -- did that accidentally get reverted?

using Microsoft.Maui.Handlers;

namespace Esri.ArcGISRuntime.Toolkit.Maui.Internal
Expand Down
Loading