How can I use the Android Exoplayer to play videos?

Kim Strasser 1,036 Reputation points
2024-07-17T12:28:29.27+00:00

Currently I use the Xamarin MediaManager to play videos: https://github.com/Baseflow/XamarinMediaManager

Is it possible to use the Android Exoplayer instead to play videos? I want to find out if the black screen that appears before a video is played will be the same when I use Android Exoplayer:

https://learn.microsoft.com/en-us/answers/questions/1820407/the-display-of-my-android-device-gets-black-for-a

How can I use the Android Exoplayer to play videos instead of the Xamarin MediaManager?

Acitivity1.cs:

_game = new Game1(this, this.Window);

Game1.cs:

 private Activity1 activity1;    
 private Android.Views.Window window;     

public async void ShowTheVideo()
   {
       VideoDialog customDialog = new VideoDialog(activity1);
       customDialog.SetCancelable(false);
       customDialog.Show();
       await CrossMediaManager.Current.Play("https://videos.pexels.com/video-files/7356431/7356431-hd_1280_720_25fps.mp4");
     
       CrossMediaManager.Current.MediaItemFinished += (s, e) =>
       {
           customDialog.Cancel();
       };
   }

VideoDialog.cs:

using Android.App;
using Android.Views;
using Android.Content;
using Android.Graphics.Drawables;
using Android.OS;
using AndroidX.Core.View;

namespace MyAndroidProject
{
    public class VideoDialog : Dialog
    {
        public VideoDialog(Context context) : base(context)
        {
        }
        protected override void OnCreate(Bundle? savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
         
            Window.RequestFeature(WindowFeatures.NoTitle);
            WindowCompat.SetDecorFitsSystemWindows(Window, false);
            WindowInsetsControllerCompat windowInsetsController = new WindowInsetsControllerCompat(Window, Window.DecorView);
            windowInsetsController.Hide(WindowInsetsCompat.Type.NavigationBars());
            windowInsetsController.SystemBarsBehavior = WindowInsetsControllerCompat.BehaviorShowTransientBarsBySwipe;
            View view = LayoutInflater.From(Context).Inflate(Resource.Layout.Activity_main, null);
            SetContentView(view);
            Window.SetBackgroundDrawable(new ColorDrawable());
            Window.SetLayout(WindowManagerLayoutParams.MatchParent, WindowManagerLayoutParams.MatchParent);
        }
    }
}

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,501 questions
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 75,581 Reputation points Microsoft Vendor
    2024-07-19T08:26:18.71+00:00

    Hello,

    I want to find out if the black screen that appears before a video is played will be the same when I use Android Exoplayer:

    This black screen caused by the following line. If you comment out this line, this black screen will disappear.

    SetContentView(_view);
    

    You application need to load the GameView by _view = _game.Services.GetService(typeof(View)) as View;, then set the game view to Activity. But Activity has a default layout when initializing.

    We cannot avoid the black screen when you SetContentView(_view). However, we can make the default layout to transparent, when you application start, you will see the transparent view, then black screen appears, you can see your game view in the end .

    Firstly, create a styles.xml in the Values folder and make sure the build action is AndroidResouce

    Then copy the following style in it. This style is set the default layout to transparent.

    <?xml version="1.0" encoding="utf-8" ?>
    <resources>
    <style name="MainTheme" parent="@android:style/Theme.Holo">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    </style>
    </resources>
    

    Next, add the Theme to the [Acitivty] tag.

    [Activity(
         Theme = "@style/MainTheme",
         Label = "@string/app_name",
         ....
     
       
    )]
    public class Activity1 : AndroidGameActivity
    

    If you want to make the Dialog without titlebar. Please change your VideoDialog's constructor like following code.

    public VideoDialog(Context context,int themeResId) : base(context, themeResId)
    {
         initView(context);
    }
    

    When you show your VideoDialog. You can set the no action bar style like following code.

      public async void ShowTheVideo()
      {
     
          VideoDialog customDialog = new VideoDialog(activity1,Android.Resource.Style.ThemeDeviceDefaultDialogNoActionBar);
    

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

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.