Xamarin - 如何设置/更改VideoView显示预览方向

时间:2017-06-07 09:45:04

标签: android xamarin xamarin.android android-videoview

我从这里复制了一段代码https://developer.xamarin.com/recipes/android/media/video/record_video/,这是关于制作视频流的说明,我正试图改变它的某些部分。

我已尝试使用SetOrientationHint(90)

将输出方向更改为90

但是因为这段代码不使用Camera类而只使用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();
    }
}

更新

@Elvis Xia的回答帮了很多 这是新代码

[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 个答案:

答案 0 :(得分:1)

  

但是因为这段代码不使用Camera类而只使用MediaRecorder类。如何旋转显示预览,因为它给了我+ 90度和横向预览?

设置轮换度后,您需要将CameraMediaRecorder相关联:

  1. 通过GetCameraInstace

    获取相机实例
    public static Camera GetCameraInstance()
    {
        Camera c = null;
        try
        {
            c = Camera.Open();
        }
        catch (Exception e)
        {
    
        }
        return c;
    }
    
  2. MainActivity.cs记录按钮点击事件中,将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);