Skip to content

Commit

Permalink
Fix Media Element Android Layout (#1814)
Browse files Browse the repository at this point in the history
The most significant changes in the code include the removal of the `WidthRequest` attribute in `MediaElementPage.xaml`, the addition of the `ResizeableActivity = true` attribute in `MainActivity.cs`, and the modification of the `OnFullscreenButtonClick` method in the `MauiMediaElement` class.

1. The `WidthRequest` attribute of the `MediaElement` toolkit was removed from `MediaElementPage.xaml`. This attribute was previously used to set the width of the media element based on the device type (tablet or default).

2. The `Activity` attribute in `MainActivity.cs` was modified to include `ResizeableActivity = true`. This change allows the activity to be resizable.

3. In `MauiMediaElement.android.cs`, the `Android.Util` namespace was added to the import statements. Two new integer variables `playerHeight` and `playerWidth` were added to the `MauiMediaElement` class. These variables are used to store the height and width of the player view.

4. The `OnFullscreenButtonClick` method of the `MauiMediaElement` class was updated. The new code stores the current player view's height and width, retrieves the display metrics, removes the player view from its current layout, and adds it to a new layout with updated parameters. The code for exiting fullscreen mode was also updated to restore the player view to its original size and layout.

5. In `MediaManager.android.cs`, the import statement for `AndroidX.CoordinatorLayout.Widget` was removed. The `LayoutParameters` attribute of the `PlayerView` object was changed from `FrameLayout.LayoutParams` to `RelativeLayout.LayoutParams`. This change aligns with the updates made in the `MauiMediaElement` class, where the player view is now added to a `RelativeLayout` instead of a `FrameLayout`.

Co-authored-by: Brandon Minnick <13558917+brminnick@users.noreply.github.com>
  • Loading branch information
ne0rrmatrix and TheCodeTraveler authored Apr 29, 2024
1 parent bcd463e commit 971842b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
</Grid.RowDefinitions>
<toolkit:MediaElement
x:Name="MediaElement"
WidthRequest="{OnIdiom Tablet=410, Default=380}"
ShouldAutoPlay="True"
Source="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
MediaEnded="OnMediaEnded"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace CommunityToolkit.Maui.Sample;

[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTask, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
[Activity(Theme = "@style/Maui.SplashTheme",ResizeableActivity =true, MainLauncher = true, LaunchMode = LaunchMode.SingleTask, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
public class MainActivity : MauiAppCompatActivity
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Android.Content;
using Android.Content.Res;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using AndroidX.CoordinatorLayout.Widget;
Expand All @@ -20,6 +21,8 @@ public class MauiMediaElement : CoordinatorLayout
int defaultSystemUiVisibility;
bool isSystemBarVisible;
bool isFullScreen;
int playerHeight;
int playerWidth;

#pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
#pragma warning disable IDE0060 // Remove unused parameter
Expand Down Expand Up @@ -131,28 +134,30 @@ void OnFullscreenButtonClick(object? sender, StyledPlayerView.FullscreenButtonCl
if (e.IsFullScreen)
{
isFullScreen = true;

playerHeight = playerView.Height;
playerWidth = playerView.Width;
DisplayMetrics displayMetrics = new DisplayMetrics();
currentWindow?.WindowManager?.DefaultDisplay?.GetMetrics(displayMetrics);
var layout = currentWindow?.DecorView as ViewGroup;

RemoveView(playerView);
RelativeLayout.LayoutParams item = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
item.Width = displayMetrics.WidthPixels;
item.Height = displayMetrics.HeightPixels;
layout?.AddView(playerView, item);
SetSystemBarsVisibility();

// Update the PlayerView
if (currentWindow.DecorView is FrameLayout layout)
{
RemoveView(playerView);
layout.AddView(playerView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent));
}
}
// Show again the SystemBars and Status bar
else
{
isFullScreen = false;
SetSystemBarsVisibility();
var layout = currentWindow?.DecorView as ViewGroup;
RelativeLayout.LayoutParams item = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
item.Width = playerWidth;
item.Height = playerHeight;

// Update the PlayerView
if (currentWindow.DecorView is FrameLayout layout)
{
layout.RemoveView(playerView);
AddView(playerView);
}
layout?.RemoveView(playerView);
AddView(playerView, item);
SetSystemBarsVisibility();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Android.Support.V4.Media.Session;
using Android.Views;
using Android.Widget;
using AndroidX.CoordinatorLayout.Widget;
using Com.Google.Android.Exoplayer2;
using Com.Google.Android.Exoplayer2.Audio;
using Com.Google.Android.Exoplayer2.Metadata;
Expand Down Expand Up @@ -45,7 +44,7 @@ public partial class MediaManager : Java.Lang.Object, IPlayer.IListener
Player = Player,
UseController = false,
ControllerAutoShow = false,
LayoutParameters = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent, GravityFlags.CenterHorizontal)
LayoutParameters = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent)
};

return (Player, PlayerView);
Expand Down

0 comments on commit 971842b

Please sign in to comment.