On The Dot...
Talking about all things .NET related

Video Playback in Your Xamarin.Android Apps - Part 1: Using VideoView

Tuesday, July 21, 2015 7:33 PM

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"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
  <VideoView android:id="@+id/PlayerVideoView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true" />
</RelativeLayout>
 

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)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);
 
        videoPlayer = FindViewById<VideoView>(Resource.Id.PlayerVideoView);
 
    }
 
    protected override void OnStart()
    {
        base.OnStart();
        LaunchVideo();
    }
 
    private void LaunchVideo()
    {
        //do the guys in this video look familiar?
        string videoUri = "http://bitly.com/1MC3Gig";
 
        videoPlayer.SetVideoURI(Android.Net.Uri.Parse(videoUri));
        videoPlayer.Start();
    }
}
 
 

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.




Feedback

# re: Video Playback in Your Xamarin.Android Apps - Part 1: Using VideoView

Dear,
i try to re-use your code and run it on xamarin player (nesus 5 lolipop ) and i got some error ( can't play this video ) like this:
[MediaPlayer] Couldn't open file on client side, trying server side
[MediaPlayer] error (1, -2147483648)
[MediaPlayer] Error (1,-2147483648)
[VideoView] Error: 1,-2147483648
[EGL_xap] eglSurfaceAttrib not implemented
[OpenGLRenderer] Failed to set EGL_SWAP_BEHAVIOR on surface 0x9e9bab80, error=EGL_SUCCESS
[OpenGLRenderer] endAllStagingAnimators on 0x9e9c2d00 (RippleDrawable) with handle 0xae839860

Please help me ! 10/22/2015 3:37 AM | moon

Post a comment