Recently, I was tasked with including video playback within a Xamarin.Android application, so I decided to write a blog post series about my approach in hopes it helps others who are building similar apps.
Incorporating videos within your Xamarin.Android application can be handled in a couple of ways. You can load and play the video in the application or you can launch the video in an external installed media player on the device. In this post, we will walk through the steps to incorporate videos right within the application using Android’s VideoView. We will develop a simple demo that loads a video from a URL.
Modifying the Android Manifest
Since we will load a video from a URL, we must add the following permission to the AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET" />
Adding the VideoView to an Activity
In order to play a video within a Xamarin.Android application, we will use a VideoView which can load and play videos from a specified path or URI.
For this post, let’s create a simple demo to illustrate the main concepts of creating a basic video player to display videos and whose orientation will change when the device is rotated. In this case, we will just use the MainActivity to display the video full screen.
Modify the MainActivity.axml file to include a VideoView within a RelativeLayout, and configure the VideoView to appear center screen as follows:
<?xml version="1.0" encoding="utf-8"?>
Within MainActivity.cs, we need to obtain a handle to the VideoView in order to specify the URI of the video to play, and then trigger the video to launch when the activity is loaded:
[Activity(Label = "VideoPlayerDemo.Droid",
MainLauncher = true,
Icon = "@drawable/icon")]
public class MainActivity : Activity
private VideoView videoPlayer;
protected override void OnCreate(Bundle bundle)
videoPlayer = FindViewById<VideoView>(Resource.Id.PlayerVideoView);
protected override void OnStart()
private void LaunchVideo()
//do the guys in this video look familiar?
string videoUri = "http://bitly.com/1MC3Gig";
At this point, the video will play and will rotate when the device is rotated, which is great! With very little effort, we were able to easily incorporate video playback within the application, but there are some additional features we can include to enhance the user experience. In Part 2 of this series, we will look at improving upon our simple video player by adding media controls and incorporating a progress indicator while the video loads.
Source: GeekswithBlogs – Lori Lalonde