Xamarin: Using VideoView with MediaController

This recipe shows how to play a video using the VideoView and MediaController.

Follow these steps to play a video:

  1. Add a VideoView to an Main.axml layout.

    01.<?xml version="1.0" encoding="utf-8"?>
    02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    03.    android:orientation="vertical"
    04.    android:layout_width="match_parent"
    05.    android:layout_height="match_parent">
    06.    <android.support.v7.widget.Toolbar
    07.        android:id="@+id/toolbar"
    08.        android:layout_width="match_parent"
    09.        android:layout_height="wrap_content"
    10.        android:background="#F8F8F8" />
    11.    <ListView
    12.        android:id="@+id/lstView"
    13.        android:layout_width="match_parent"
    14.        android:layout_height="match_parent"
    15.        android:background="@android:color/white" />
    16.</LinearLayout>
    
  2. Obtain a reference to the VideoView in code.

    1.var video_view = FindViewById<VideoView>(Resource.Id.videoView);
    
  3.  Create an Android.Net.Uri for the video.

    1.var uri = Android.Net.Uri.Parse ("video url goes here");
    
  4.  Pass this URI to the VideoView.

    1.video_view.SetVideoURI (uri);
    
  5.  Create an MediaController for VideoView.

    1.private MediaController media_controller;
    
  6. Set MediaController to the VideoView.

    1.media_controller = new  Android.Widget.MediaController(this);
    2.media_controller.SetMediaPlayer(video_view);
    3.video_view.SetMediaController(media_controller);
    4.video_view.RequestFocus();
    
  7. Start the VideoView.

    1.video_view.Start();
    
  8. In AndroidManifest, we set the INTERNET permission and press OK.