Is it possible to play videos on Android with a native video player?

Kim Strasser 1,036 Reputation points
2024-06-22T10:01:05.73+00:00

In one project the following code for Xamarin MediaManager works. But I have another project where I use the same code and there it is not working. The problem is that the video is not correctly displayed in full screen because there is a small free area between the video and the Android bar at the bottom of the screen in landscape mode.

In addition, the video view is not always removed from display after the video has played until the end. Sometimes customDialog.Cancel() is not executed.

I don´t want that the Android bar is displayed at the bottom of my tablet when a video is displayed.

I use SetImmersive() to remove the Android bar in my game but the problem is that the Android bar is displayed again when I start playing a video.

What can I do so that the Android bar is not displayed when I play a video?

Is it possible to use a native video player on Android instead of the Xamarin MediaManager? Maybe a native video player would work better.

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <mediamanager.platforms.android.video.VideoView
        android:id="@+id/exoplayerview_activity_video"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true"
        android:layout_centerInParent="true" />
</RelativeLayout>

VideoDialog.cs:

using Android.App;
using Android.Views;
using Android.Content;
using Android.Graphics.Drawables;
using Android.OS;

namespace TestPluginMedia
{
    public class VideoDialog : Dialog
    {
        public VideoDialog(Context context) : base(context)
        {
        }
        protected override void OnCreate(Bundle? savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Window.RequestFeature(WindowFeatures.NoTitle);
            View view = LayoutInflater.From(Context).Inflate(Resource.Layout.activity_main, null);
            SetContentView(view);
            Window.SetBackgroundDrawable(new ColorDrawable());
            Window.SetLayout(WindowManagerLayoutParams.MatchParent, WindowManagerLayoutParams.MatchParent);
        }
    }
}

Acitivity1.cs:

using Android.App;
using Android.Content.PM;
using Android.OS;
using Android.Views;
using MediaManager;
using Microsoft.Xna.Framework;
using SharedCode;

namespace TestPluginMedia 
{     
[Activity(         
Label = "@string/app_name",         
MainLauncher = true,         
Icon = "@drawable/icon",         
AlwaysRetainTaskState = true,         
LaunchMode = LaunchMode.SingleInstance,         
ScreenOrientation = ScreenOrientation.FullUser,         
ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden | ConfigChanges.ScreenSize     
)]     

public class Activity1 : AndroidGameActivity     
{         
private Game1 _game;         
private View _view;    
      
protected override void OnCreate(Bundle bundle)
{                         
    base.OnCreate(bundle);       

    // SetContentView(Resource.Layout.activity_main);               

    CrossMediaManager.Current.Init(this);
                        
    _game = new Game1(this);
    _view = _game.Services.GetService(typeof(View)) as View;                             
    SetContentView(_view);                          
    _game.Run();         
    SetImmersive();
}   


private void SetImmersive()   
{               
       string except = string.Empty;
       if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.R)
       {
           try
           {
           Window.SetDecorFitsSystemWindows(false);
           var it = Window.InsetsController;
           it.Hide(WindowInsets.Type.NavigationBars());
           it.Hide(WindowInsets.Type.StatusBars());
           it.Hide(WindowInsets.Type.SystemBars());
           it.SystemBarsBehavior = (int)WindowInsetsControllerBehavior.ShowTransientBarsBySwipe;
           }
           catch (Exception ex)
           {
               except = ex.ToString();
           }
       }
}

protected override void OnResume()
{
    base.OnResume();
  
    SetImmersive();
}

public override void OnWindowFocusChanged(bool hasFocus)
{
      base.OnWindowFocusChanged(hasFocus);
   
      if (hasFocus)
          SetImmersive();
}

public void OnGlobalFocusChanged(View oldFocus, View newFocus)
{
     SetImmersive();
}
   
} 
}

Game1.cs:

public class Game1 : Game  
{       
      private GraphicsDeviceManager _graphics;       
      private SpriteBatch _spriteBatch;       
      private Activity1 activity1;       
      public Game1(Activity1 activity1)
      {
          this.activity1 = activity1;
          _graphics = new GraphicsDeviceManager(this);
          Content.RootDirectory = "Content";
          
      }

    public async void ShowTheVideo()
    {
 
     VideoDialog customDialog = new VideoDialog(activity1);
     customDialog.SetCancelable(false);
     customDialog.Show();
     await CrossMediaManager.Current.Play("https://sec.ch9.ms/ch9/5d93/a1eab4bf-3288-4faf-81c4-294402a85d93/XamarinShow_mid.mp4");
     CrossMediaManager.Current.MediaItemFinished += (s, e) =>
     {
         customDialog.Cancel();
     };
    }

     protected override async void LoadContent()
     {             
         _spriteBatch = new SpriteBatch(GraphicsDevice);          
         ShowTheVideo();         
     }
}
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,623 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 76,631 Reputation points Microsoft Vendor
    2024-06-25T02:10:13.66+00:00

    Hello,

    How can I hide this bar in my game when a video is playing?

    Firstly, you add Activity's Window to the Game1.s constructor. When you execute the ShowTheVideo method, you can hide the bar by windowInsetsController.Hide(WindowInsetsCompat.Type.NavigationBars()); windowInsetsController.SystemBarsBehavior = WindowInsetsControllerCompat.BehaviorShowTransientBarsBySwipe;

    You can refer to the following code, If the video is finished, this bar will show again. If you want to full screen, please move code about showing system bars in the MediaItemFinished event.

        public class Game1 : Game
        {
            private GraphicsDeviceManager _graphics;
            private SpriteBatch _spriteBatch;
            private Activity1 activity1;
            private Window window;
     
        
     
            public Game1(Activity1 activity1, Window window) 
            {
                this.window = window;
     
                this.activity1 = activity1;
                _graphics = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";
                
            }
    
    public async void ShowTheVideo()
    {
     
         VideoDialog customDialog = new VideoDialog(activity1);
         customDialog.SetCancelable(false);
         customDialog.Show();
         await CrossMediaManager.Current.Play("https://sec.ch9.ms/ch9/5d93/a1eab4bf-3288-4faf-81c4-294402a85d93/XamarinShow_mid.mp4");
     
         // Hide system bars
         WindowCompat.SetDecorFitsSystemWindows(window, false);
         WindowInsetsControllerCompat windowInsetsController = new WindowInsetsControllerCompat(window, window.DecorView);
         windowInsetsController.Hide(WindowInsetsCompat.Type.NavigationBars());
         windowInsetsController.SystemBarsBehavior = WindowInsetsControllerCompat.BehaviorShowTransientBarsBySwipe;
     
         CrossMediaManager.Current.MediaItemFinished += (s, e) =>
         {
             customDialog.Cancel();
     
             // show system bars
             WindowCompat.SetDecorFitsSystemWindows(window, true);
             WindowInsetsControllerCompat windowInsetsController = new WindowInsetsControllerCompat(window, window.DecorView);             
             windowInsetsController.Show(WindowInsetsCompat.Type.NavigationBars());             
         };
    }
    

    In the end, if you init the Game1. Please add this.Window in _game = new Game1(this,this.Window); line.

      protected override void OnCreate(Bundle bundle)
      {
          base.OnCreate(bundle);
          CrossMediaManager.Current.Init(this);
          _game = new Game1(this,this.Window);
    ....
      }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.