2017-06-07 8 views
0

ここからhttps://developer.xamarin.com/recipes/android/media/video/record_video/のコードをコピーしてビデオストリームを作成しましたが、その一部を変更しようとしています。Xamarin - VideoView表示のプレビューオリエンテーションを設定/変更する方法

私はすでにSetOrientationHintを使用して90に出力向きを変更しようとした(90)

しかし、このコードは、CameraクラスとだけMediaRecorderクラスを使用していないので。表示プレビューを90度回転させてプレビューを表示するので、プレビューをどのように回転させることができますか?

私はすでにxmlとコードでローテーションを試しましたが、プレビューは完全に黒くなりました。

この

は@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; 

    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です。ここ
は、新しいコード

[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度回転させてプレビューを表示するので、プレビューをどのように回転させることができますか?

  1. GetCameraInstaceを介してカメラInstacnceを得る:MainActivity.cs録画ボタンクリックイベントで

    public static Camera GetCameraInstance() 
    { 
        Camera c = null; 
        try 
        { 
         c = Camera.Open(); 
        } 
        catch (Exception e) 
        { 
    
        } 
        return c; 
    } 
    
  2. を関連付けるあなたが回転度を設定した後MediaRecorderCameraを関連付ける必要があり

cameraMediaRecorderとし、向きをmCamera.Unlock

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」の「カメラ」。 'Utility'は' GetCameraInstance'静的メソッドをカプセル化するために使用したクラスです。 –

+0

私はすでにそれを試して、それは私の問題を向きで解決しました。他の人には:プレビューを止めてカメラを放すことも忘れないでください。 – jace

関連する問題