Java 如何在SurfaceView上预览捕获的视频?

Java 如何在SurfaceView上预览捕获的视频?,java,android,surfaceview,android-mediarecorder,surfaceholder,Java,Android,Surfaceview,Android Mediarecorder,Surfaceholder,我用这段代码捕获了一段视频,并在表面上预览了它。但是有一些逻辑错误。摄像机不捕捉视频。我不知道;我不明白是什么错误 public class VideoCapture extends Activity implements View.OnClickListener,SurfaceHolder.Callback { MediaRecorder recorder; SurfaceHolder holder; boolean recording =

我用这段代码捕获了一段视频,并在表面上预览了它。但是有一些逻辑错误。摄像机不捕捉视频。我不知道;我不明白是什么错误

public class VideoCapture extends Activity implements    View.OnClickListener,SurfaceHolder.Callback          
{

    MediaRecorder recorder;
    SurfaceHolder holder;
    boolean recording = false;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        recorder = new MediaRecorder();
        initRecorder();
        setContentView(R.layout.activity_video_capture);

        SurfaceView cameraView = (SurfaceView) findViewById(R.id.CameraView);
        holder = cameraView.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        cameraView.setClickable(true);
        cameraView.setOnClickListener(this);
    }

    private void initRecorder() {
        recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

        CamcorderProfile cpHigh = CamcorderProfile
                .get(CamcorderProfile.QUALITY_HIGH);
        recorder.setProfile(cpHigh);
        recorder.setOutputFile("/sdcard/videocapture_example.mp4");
        recorder.setMaxDuration(50000); // 50 seconds
        recorder.setMaxFileSize(5000000); // Approximately 5 megabytes
    }

    private void prepareRecorder() {
        recorder.setPreviewDisplay(holder.getSurface());

        try {
            recorder.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
            finish();
        } catch (IOException e) {
            e.printStackTrace();
            finish();
        }
    }

    public void onClick(View v) {
        if (recording) {
            recorder.stop();
            recording = false;

            // Let's initRecorder so we can record again
            initRecorder();
            prepareRecorder();
        } else {
            recording = true;
            recorder.start();
        }
    }

    public void surfaceCreated(SurfaceHolder holder) {
        prepareRecorder();
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        if (recording) {
            recorder.stop();
            recording = false;
        }
        recorder.release();
        finish();
    }
}
XML文件是

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<SurfaceView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/CameraView"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true" />
</RelativeLayout>

清单文件

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />


您能否详细说明运行此代码时发生了什么和没有发生什么?另外,您进行了什么样的故障排除?@Julie当我试图用手机打开它时,屏幕显示为黑色,但创建的视频文件大小为0B。我有权限访问相机,但它仍然不工作。我之前不确定是相机坏了还是表面坏了,但结果是“相机”。至于故障排除,我检查了手机是否堵塞了摄像头,但事实并非如此。这段代码早些时候运行得很好,但不知从何而来,它已经停止了交付。您能否详细介绍一下运行这段代码时到底发生了什么和没有发生什么?另外,您进行了什么样的故障排除?@Julie当我试图用手机打开它时,屏幕显示为黑色,但创建的视频文件大小为0B。我有权限访问相机,但它仍然不工作。我之前不确定是相机坏了还是表面坏了,但结果是“相机”。至于故障排除,我检查了手机是否堵塞了摄像头,但事实并非如此。这段代码早些时候运行得很好,但不知从何而来,它已经停止了交付。