-
Notifications
You must be signed in to change notification settings - Fork 129
Add Android support for MauiMediaElementHandler #742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) | ||
| { | ||
| 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(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.