title: Xamarin - How to set/change VideoView display preview orientation
body: <p>I copied a code from here <a href="https://developer.xamarin.com/recipes/android/media/video/record_video/" rel="nofollow noreferrer">https://developer.xamarin.com/recipes/android/media/video/record_video/</a>, an instruction on making videostream and I am trying to change some part of it.</p>
  
  <p>I already tried to change the output orientation to 90 using SetOrientationHint(90)</p>
  
  <p>But since this code do not use the Camera class and just a MediaRecorder class. How can I rotate the display preview because it is giving me a +90degrees and landscape preview?</p>
  
  <p>I already tried the rotation in xml and code but the preview became a total black.</p>
  
  <p>This is the code</p>
  
  <pre><code>[Activity(Label = "App2", MainLauncher = true, Icon = "@drawable/icon")]
  public class MainActivity : Activity, ISurfaceHolderCallback
  {
      string path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/test.mp4";
      MediaRecorder recorder;
  
      protected override void OnCreate(Bundle bundle)
      {
          base.OnCreate(bundle);
          //Set our view from the "main" layout resource
          SetContentView(Resource.Layout.Main);
          var record = FindViewById&lt;Button&gt;(Resource.Id.Record);
          var stop = FindViewById&lt;Button&gt;(Resource.Id.Stop);
          var play = FindViewById&lt;Button&gt;(Resource.Id.Play);
          var video = FindViewById&lt;VideoView&gt;(Resource.Id.SampleVideoView);
          //video.Rotation = 90;
  
          record.Click += delegate
          {
              if (recorder == null)
                  recorder = startRecording(video);
              else
                  Toast.MakeText(this, "Now recording", 0).Show();
          };
  
          stop.Click += delegate
          {
              if (recorder != null)
              {
                  stopRecording(recorder);
                  recorder = null;
              }
              else
                  Toast.MakeText(this, "No video recording", 0).Show();
          };
  
          play.Click += delegate
          {
              if (path != null)
                  playVideo(video);
              else
                  Toast.MakeText(this, "No video available", 0).Show();
          };
  
          //recorder = startRecording(video);
      }
  
      protected override void OnDestroy()
      {
          base.OnDestroy();
  
          if (recorder != null)
          {
              recorder.Release();
              recorder.Dispose();
              recorder = null;
          }
      }
  
      private void playVideo(VideoView video)
      {
          var uri = Android.Net.Uri.Parse(path);
          video.SetVideoURI(uri);
          video.Start();
      }
  
      private static void stopRecording(MediaRecorder recorder)
      {
          if (recorder != null)
          {
              recorder.Stop();
              recorder.Release();
          }
      }
  
      private MediaRecorder startRecording(VideoView video)
      {
          MediaRecorder recorder;
          video.StopPlayback();
  
          //video.Holder.AddCallback(this);
          //video.Holder.SetType(SurfaceType.PushBuffers);
  
          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(path);
          recorder.SetOrientationHint(90);
          recorder.SetPreviewDisplay(video.Holder.Surface);
          if (recorder!=null)
          {
              try
              {
                  recorder.Prepare();
                  recorder.Start();
              }
              catch (Exception)
              {
                  Toast.MakeText(this, "Exception!", 0).Show();
              }
          }
          return recorder;
      }
  
      public void SurfaceChanged(ISurfaceHolder holder, [GeneratedEnum] Format format, int width, int height)
      {
          throw new NotImplementedException();
      }
  
      public void SurfaceCreated(ISurfaceHolder holder)
      {
          throw new NotImplementedException();
      }
  
      public void SurfaceDestroyed(ISurfaceHolder holder)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>@Elvis Xia's answer helped a lot.<br>
  Here is the new code</p>
  
  <pre><code>[Activity(Label = "App2", MainLauncher = true, Icon = "@drawable/icon")]
  public class MainActivity : Activity, ISurfaceHolderCallback
  {
      string path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/test.mp4";
      MediaRecorder recorder;
      Android.Hardware.Camera mCamera; //Android.Hardware is used because it will have 
                                       //problem with Android.Graphics
  
      protected override void OnCreate(Bundle bundle)
      {
          base.OnCreate(bundle);
          //Set our view from the "main" layout resource
          SetContentView(Resource.Layout.Main);
          var record = FindViewById&lt;Button&gt;(Resource.Id.Record);
          var stop = FindViewById&lt;Button&gt;(Resource.Id.Stop);
          var play = FindViewById&lt;Button&gt;(Resource.Id.Play);
          var video = FindViewById&lt;VideoView&gt;(Resource.Id.SampleVideoView);
  
          record.Click += delegate
          {
              if (recorder == null)
                  recorder = startRecording(video);
              else
                  Toast.MakeText(this, "Now recording", 0).Show();
          };
  
          stop.Click += delegate
          {
              if (recorder != null)
              {
                  stopRecording(recorder, mCamera);
                  recorder = null;
              }
              else
                  Toast.MakeText(this, "No video recording", 0).Show();
          };
  
          play.Click += delegate
          {
              if (path != null)
                  playVideo(video);
              else
                  Toast.MakeText(this, "No video available", 0).Show();
          };
  
          //recorder = startRecording(video);
      }
  
      protected override void OnDestroy()
      {
          base.OnDestroy();
  
          if (recorder != null)
          {
              recorder.Release();
              recorder.Dispose();
              recorder = null;
          }
      }
  
      private void playVideo(VideoView video)
      {
          var uri = Android.Net.Uri.Parse(path);
          video.SetVideoURI(uri);
          video.Start();
      }
  
      private static void stopRecording(MediaRecorder recorder, Android.Hardware.Camera mCamera)
      {
          if (recorder != null)
          {
              recorder.Stop();
              recorder.Release();
              mCamera.StopPreview();
              mCamera.Release();
          }
      }
  
      private MediaRecorder startRecording(VideoView video)
      {
          MediaRecorder recorder;
          video.StopPlayback();
  
          //video.Holder.AddCallback(this);
          //video.Holder.SetType(SurfaceType.PushBuffers);
  
          recorder = new MediaRecorder();
          mCamera = GetCameraInstance();
          mCamera.SetDisplayOrientation(90);
          mCamera.Unlock();
          recorder.SetCamera(mCamera);
          recorder.SetVideoSource(VideoSource.Camera);
          recorder.SetAudioSource(AudioSource.Mic);
          recorder.SetOutputFormat(OutputFormat.Default);
          recorder.SetVideoEncoder(VideoEncoder.Default);
          recorder.SetAudioEncoder(AudioEncoder.Default);
          recorder.SetOutputFile(path);
          recorder.SetOrientationHint(90);
          recorder.SetPreviewDisplay(video.Holder.Surface);
          if (recorder!=null)
          {
              try
              {
                  recorder.Prepare();
                  recorder.Start();
              }
              catch (Exception)
              {
                  Toast.MakeText(this, "Exception!", 0).Show();
              }
          }
          return recorder;
      }
  
      public void SurfaceChanged(ISurfaceHolder holder, [GeneratedEnum] Format format, int width, int height)
      {
          throw new NotImplementedException();
      }
  
      public void SurfaceCreated(ISurfaceHolder holder)
      {
          throw new NotImplementedException();
      }
  
      public void SurfaceDestroyed(ISurfaceHolder holder)
      {
          throw new NotImplementedException();
      }
  
      public static Android.Hardware.Camera GetCameraInstance()
      {
          Android.Hardware.Camera c = null;
          try
          {
              c = Android.Hardware.Camera.Open();
          }
          catch (Exception e)
          {
  
          }
          return c;
      }
  }
  </code></pre>