title: Play video with Xamarin Forms
body: <p>We're building an app in Xamarin using <code>Xamarin.forms</code>. In the app we need to play a video, so we wrote some code for that. However, the video isn't playing and on Android the app crashes whilst throwing a generic error.</p>
  
  <p>This is the code:</p>
  
  <p><strong>VideoContainer.cs</strong></p>
  
  <pre><code>using System;
  using Xamarin.Forms;
  using System.Collections.Generic;
  
  namespace MyApp
  {
      public class VideoView : View
      {
          public Action StopAction;
          public VideoView ()
          {
              Console.WriteLine("VideoView loaded");
          }
  
          public static readonly BindableProperty FileSourceProperty = 
              BindableProperty.Create&lt;VideoView,string&gt;(
                  p =&gt; p.FileSource,string.Empty);
  
          public string FileSource {
              get { return (string)GetValue (FileSourceProperty); }
              set { SetValue (FileSourceProperty, value); }
          }
  
          public void Stop(){
              if(StopAction != null)
                  StopAction ();
          }
      }
  }
  </code></pre>
  
  <p><strong>VideoViewRender.cs</strong></p>
  
  <pre><code>using System.Text;
  
  using Android.App;
  using Android.Content;
  using Android.OS;
  using Android.Runtime;
  using Android.Views;
  using Android.Widget;
  using Xamarin.Forms;
  using Xamarin.Forms.Platform.Android;
  using Android.Media;
  using Android.Content.Res;
  using Java.Lang;
  using MyApp.Droid;
  
  [assembly: ExportRenderer(typeof(VideoView), typeof(VideoViewRenderer))]
  //
  namespace MyApp.Droid
  {
      public class VideoViewRenderer : ViewRenderer
      {
          VideoView videoview;
          MediaPlayer player;
          MediaController mediaController;
          Handler handler = new Handler();
          public VideoViewRenderer()
          {
              Console.WriteLine("VideoViewRenderer loaded");
          }   
  
          public void SurfaceChanged(ISurfaceHolder holder, global::Android.Graphics.Format format, int width, int height)
          {
  
          }
  
          public void SurfaceDestroyed(ISurfaceHolder holder)
          {
  
          }
  
          void play(string fullPath)
          {
              AssetFileDescriptor afd = Forms.Context.Assets.OpenFd(fullPath);
              if (afd != null)
              {
  
                  player.SetDataSource(afd.FileDescriptor, afd.StartOffset, afd.Length);
                  player.Prepare();
                  player.Start();
                  Control.Layout(0, 200, 400, 600);
                  player.Pause();
  
  
              }
          }
  
          public void SurfaceCreated(ISurfaceHolder holder)
          {
              player.SetDisplay(holder);
          }
  
          public override bool OnTouchEvent(MotionEvent e)
          {
              mediaController.Show();
              return false;
          }
  
          //--MediaPlayerControl methods----------------------------------------------------
          public void Start()
          {
              player.Start();
          }
  
  
          public void Pause()
          {
              player.Pause();
          }
  
          public int Duration
          {
              get
              {
                  return player.Duration;
              }
          }
  
          public int CurrentPosition
          {
              get
              {
                  return player.CurrentPosition;
              }
          }
  
          public void SeekTo(int i)
          {
              player.SeekTo(i);
          }
  
          public bool IsPlaying
          {
              get
              {
                  return player.IsPlaying;
              }
          }
  
          public int BufferPercentage
          {
              get
              {
                  return 0;
              }
          }
  
          public int AudioSessionId
          {
              get
              {
                  return 0;
              }
          }
  
          public bool CanPause()
          {
              return true;
          }
  
          public bool CanSeekBackward()
          {
              return true;
          }
  
          public bool CanSeekForward()
          {
              return true;
          }
          //--------------------------------------------------------------------------------
  
      }
  }
  </code></pre>
  
  <p>What happens is that <code>VideoView loaded</code> is logged to the console, but <code>VideoViewRenderer loaded</code> isn't. We got this code from <a href="https://forums.xamarin.com/discussion/23931/android-video-player-using-xamarin-forms" rel="nofollow">the Xamarin forum</a>, but were not able to implement it successfully. what are we doing wrong?</p>