title: Xamarin- how to record video and save it to a file
body: <p>I am trying to record a video and save it to a file but I keep getting an error when it comes to the recording. The code is </p>
  
  <pre><code>using System;         
  using Android.App;
  using Android.OS;
  using Android.Widget;
  using Android.Media;
  using System.Timers;
  using Android.Content;
  using System.IO;
  
  namespace ShowHouseDemo2._1
  {
      [Activity(Label = "VideoPage", Icon = "@drawable/icon")]
  //Intent intent = GetInTent();
  class VideoPage : Activity
  {
  
      //Intent string Vid = Intent.GetStringExtra("VidSent");
      MediaRecorder recorder;
      Timer StartTimer;
      int StartCount = 0;
      protected override void OnCreate(Bundle bundle)
      {
  
          base.OnCreate(bundle);
          SetContentView(Resource.Layout.VidTakerPage);
  
          var video = FindViewById&lt;VideoView&gt;(Resource.Id.UserVid);
          //string  path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath.   "/UserVid4.mp4";
          // string path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + ;
          var documents = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
          var filename = Path.Combine(documents, "Write.txt");
          File.WriteAllText(filename, "Write this text into a file!");
  
          video.StopPlayback();
  
  
          recorder = new MediaRecorder();
          recorder.SetVideoSource(VideoSource.Camera);
          recorder.SetAudioSource(AudioSource.Mic);
          recorder.SetOutputFormat(OutputFormat.Default);
          recorder.SetVideoEncoder(VideoEncoder.Default);
          recorder.SetAudioEncoder(AudioEncoder.Default);
          recorder.SetOutputFile(filename);
          recorder.SetPreviewDisplay(video.Holder.Surface);
          recorder.Prepare();
          recorder.Start();
  
          timerStarted();
      }
  
      private void timerStarted()
      {
          base.OnResume();
          StartTimer = new Timer();
          StartTimer.Interval = 1000;
          StartTimer.Elapsed += delegate
          {
              while (StartCount &lt; 10)
              {
                  if (StartCount == 9)
                  {
                      RunOnUiThread(() =&gt;
                      {
                          Intent k = new Intent(this, typeof(TakeVideo));
  
                          this.StartActivity(k);
                      });
                  }
                  StartCount++;
              }
          };
      }
  }
  }
  </code></pre>
  
  <p>the Error that I get is :</p>
  
  <pre><code>Unhandled Exception: 
  Java.IO.IOException: invalid preview surface.
  </code></pre>
  
  <p>I love any bit of help you can give me. Any questions that you have please ask</p>