How can I find out if my video view is on top of my game view in my Android .NET 7.0 project?

Kim Strasser 686 Reputation points
2024-06-09T21:02:21.89+00:00

I want to play a video in my Android .NET 7.0 project. I have installed the NuGet package XamarinMediaManager version 1.2.2: https://github.com/Baseflow/XamarinMediaManager

But the problem is that I only hear the sound of the video when I debug on my Android tablet. I don't know why the video is not displayed.

Is it possible that the video view is under the game view and that I cannot see the video because my game is drawn on top of the video? Or could it be another problem?

Activity1.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);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            CrossMediaManager.Current.Init(this);
            CrossMediaManager.Current.MediaPlayer.ShowPlaybackControls = true;
            CrossMediaManager.Current.AutoPlay = true;

            var videoView = FindViewById<MediaManager.Platforms.Android.Video.VideoView>(Resource.Id.exoplayerview_activity_video);
            CrossMediaManager.Current.MediaPlayer.VideoView = videoView;
                     
            _game = new Game1();             
            _view = _game.Services.GetService(typeof(View)) as View;                  
            SetContentView(_view);             
            _game.Run();         
}     
} 
}

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="300dp"
        android:focusable="true"
        android:layout_centerInParent="true" />
</RelativeLayout>

Game1.cs:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MediaManager;
using Microsoft.Xna.Framework.Media;

namespace SharedCode
{
    public class Game1 : Game
    {
        private GraphicsDeviceManager _graphics;
        private SpriteBatch _spriteBatch;
        
        public Game1()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
        }
        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override async void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);
            await CrossMediaManager.Current.Play("https://archive.org/download/BigBuckBunny_328/BigBuckBunny_512kb.mp4");
        }

        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            base.Draw(gameTime);
        }
    }
}

.csproj:

 <TargetFramework>net7.0-android33.0</TargetFramework>
    <SupportedOSPlatformVersion>31.0</SupportedOSPlatformVersion>
    <OutputType>Exe</OutputType>
    <UseMaui>true</UseMaui>
    <UseMauiEssentials>true</UseMauiEssentials>
...
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,070 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 37,751 Reputation points Microsoft Vendor
    2024-06-10T08:16:32.3466667+00:00

    Hello,

    I noticed that you used SetContentView twice in the OnCreate method, and the second time you used Game as View, your MediaPlayer wasn't in that View.

    Also, the second time you use SetContentView, the Game's widget is a View, not a ViewGroup. This means that there is only one unique control in the Game, so it's not a matter of a layer, but rather that there is no MediaPlayer control in the ContentView.

    You can try putting the Game and MediaPlayer in the same Layout and then displaying them together via SetContentView.

    Update:

    After testing, it is not a good idea to use stacked views to control video playback. This will lead to the following major problems.

    • Placing Game and MediaPlayer in the same view will result in a larger memory footprint.
    • It is difficult to achieve a smooth transition animation when selecting a video to play.

    Therefore, it is a better choice to use a pop-up window to isolate the two views and then pop up the MediaPlayer through the play event in the Game.

    The following is the core code to implement this function.

    public class VideoDialog : Dialog
    {
        public VideoDialog(Context context) : base(context) {
            initView();
        }
     
        private void initView()
        {
            SetContentView(Resource.Layout.VideoLayout);
            // setting width and height
            WindowManagerLayoutParams layoutParams = Window.Attributes;
            layoutParams.Width = WindowManagerLayoutParams.MatchParent;
            layoutParams.Height = WindowManagerLayoutParams.WrapContent;
            Window.Attributes=layoutParams;
        }
    }
    

    Show the dialog in event:

    button.Click += (s, e) =>
    {
        VideoDialog customDialog = new VideoDialog(this);
        customDialog.Show();
    };
    

    Update:

    For the Dialog's full screen problem, you can use the following method.

    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.VideoLayout, null);
            SetContentView(view);
            Window.SetBackgroundDrawable(new ColorDrawable());
            Window.SetLayout(WindowManagerLayoutParams.MatchParent, WindowManagerLayoutParams.MatchParent);
        }
    }
    

    Best Regards,

    Alec Liu.


    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.