2017-06-07 53 views
0

我從這裏複製了一個代碼https://developer.xamarin.com/recipes/android/media/video/record_video/,這是一個關於製作videostream的指令,我試圖更改它的某些部分。Xamarin - 如何設置/更改VideoView顯示屏預覽方向

我已經嘗試過使用SetOrientationHint(90)

輸出方向改變爲90,但由於這個代碼不使用相機類,只需MediaRecorder類。我怎樣才能旋轉顯示預覽,因爲它給了我+ 90度和景觀預覽?

我已經嘗試過在xml和代碼中旋轉,但預覽變成了全黑。

這是代碼

[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<Button>(Resource.Id.Record); 
     var stop = FindViewById<Button>(Resource.Id.Stop); 
     var play = FindViewById<Button>(Resource.Id.Play); 
     var video = FindViewById<VideoView>(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(); 
    } 
} 

UPDATE

@Elvis夏的回答幫了不少忙。
這是新代碼

[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<Button>(Resource.Id.Record); 
     var stop = FindViewById<Button>(Resource.Id.Stop); 
     var play = FindViewById<Button>(Resource.Id.Play); 
     var video = FindViewById<VideoView>(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; 
    } 
} 

回答

1

但由於該代碼不使用相機類,只需MediaRecorder類。我怎樣才能旋轉顯示預覽,因爲它給了我+ 90度和景觀預覽?

您需要設置的旋轉程度後CameraMediaRecorder關聯:

  1. 得到一個攝像頭Instacnce通過GetCameraInstace

    public static Camera GetCameraInstance() 
    { 
        Camera c = null; 
        try 
        { 
         c = Camera.Open(); 
        } 
        catch (Exception e) 
        { 
    
        } 
        return c; 
    } 
    
  2. MainActivity.cs記錄按鈕點擊事件,關聯的cameraMediaRecorder並在之前設置方向:

    Camera mCamera 
    ... 
    
    recorder = new MediaRecorder(); 
    mCamera = ClassName.GetCameraInstance(); //ClassName if in different class. 
                 //else just GetCameraInstance(); 
    mCamera.SetDisplayOrientation(90); 
    mCamera.Unlock(); 
    recorder.SetCamera(mCamera); 
    
    recorder.SetVideoSource(VideoSource.Camera); 
    
+0

是該相機是從類Android.Hardware? 。和Utility.GetCameraInstance()中的實用程序是什麼?你的班名是? – jace

+0

是的。 'Android.Hardware'中的'Camera'。 'Utility'只是一個我用來封裝'GetCameraInstance'靜態方法的類。 –

+0

我已經試過了,它解決了我的定位問題:)感謝您的幫助。對於其他人:不要忘記停止預覽並釋放相機。 – jace