Implement The Video Player In Xamarin.Android

Introduction

In this article, I will explain the video player feature in android. Wwe can develop the video player in Xamarin.Android with help of media controller and video view classes. Video view can help to load images from various sources and computing measurements from the video. We can use any layout manager for playing video with help of videoview.

Important Note

VideoView does not retain its full state when going into the background. It does not restore the current play position and its play state. Applications should save and restore these in onSaveInstanceState (Bundle) and onRestoreInstanceState(Bundle).

Step 1

Create a new res XML file and name it as you wish. Please follow the below code to define videoview.

<?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"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
         <VideoView
                  android:id="@+id/videoView1"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_alignParentBottom="true"
                  android:layout_alignParentLeft="true"
                  android:layout_alignParentRight="true"
                  android:layout_alignParentTop="true" />
         <Button
                  android:id="@+id/btnplay"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:text="Play"
                  android:gravity="center"/>
</RelativeLayout>

Step 2

Save the video files in drawable folder and make it a bundle resource. After that, you can use below code to set the path of video playing.

VideoView videoView = (VideoView)FindViewById(Resource.Id.videoView1);
Uri uri = Uri.Parse("android.resource://" + PackageName + "/" + Resource.Drawable.sample1);

Step 3

Create the MediaControl for playing the video with pause/play options and use the below code.

//Creating MediaController
MediaController mediaController = new MediaController(this);
mediaController.SetAnchorView(videoView);

Step 4

Use the below full code for achieving the video player for android. We get the video from path or URL.

[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity: AppCompatActivity {
    MediaController mediaController = null;
    protected override void OnCreate(Bundle savedInstanceState) {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);
        VideoView videoView = (VideoView) FindViewById(Resource.Id.videoView1);
        Button Button = (Button) FindViewById(Resource.Id.btnplay);
        Button.Click += delegate {
            this.Window.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.Fullscreen);
            this.SupportActionBar.Hide();
            Button.Visibility = Android.Views.ViewStates.Gone;
            //Creating MediaController 
            mediaController = new MediaController(this);
            mediaController.SetAnchorView(videoView);
            //specify the location of media file 
            Uri uri = Uri.Parse("android.resource://" + PackageName + "/" + Resource.Drawable.sample1);
            //Setting MediaController and URI, then starting the videoView 
            videoView.SetMediaController(mediaController);
            videoView.SetVideoURI(uri);
            videoView.RequestFocus();
            videoView.Start();
        };
    }
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

Output

Conclusion

Hopefully, this article has given you sufficient information to create a Video View control for playing on Android. Feel free to leave a comment if you would like me to further elaborate on anything within this article.