使用 MediaRecorder 录制不起作用

Recording with MediaRecorder doesn't work

我正在尝试使用 MediaRecorder class 录制视频,我尝试了很多示例,但它们都有同样的问题,它们录制几秒钟,然后预览停止几秒钟,这种情况发生了几次,之后预览完全卡住了,然后我得到以下输出:

04-05 15:53:59.257 26730-26730/? D/dalvikvm: Late-enabling CheckJNI
04-05 15:54:01.312 26730-26730/com.example.sition.videopoc D/libEGL: loaded /system/lib/egl/libEGL_MRVL.so
04-05 15:54:01.351 26730-26730/com.example.sition.videopoc D/libEGL: loaded /system/lib/egl/libGLESv1_CM_MRVL.so
04-05 15:54:01.359 26730-26730/com.example.sition.videopoc D/libEGL: loaded /system/lib/egl/libGLESv2_MRVL.so
04-05 15:54:01.375 26730-26730/com.example.sition.videopoc D/GC: <tid=26730> OES20 ===> GC Version   : GC Ver SS_rls_pxa988_JB42_R1_RC2_GC13.16
04-05 15:54:01.421 26730-26730/com.example.sition.videopoc D/OpenGLRenderer: Enabling debug mode 0
04-05 15:54:01.437 26730-26730/com.example.sition.videopoc I/MediaRecorderJNI: prepare: surface=0x5ac59450 (identity=2933)
04-05 15:54:01.531 26730-26730/com.example.sition.videopoc D/v_gal: [tid=26730] gralloc_register_buffer: ===>Width = 800, Height = 480, surface = 0x5ac75c38
04-05 15:54:01.585 26730-26730/com.example.sition.videopoc D/v_gal: [tid=26730] gralloc_register_buffer: ===>Width = 800, Height = 480, surface = 0x5ac44360
04-05 15:54:02.789 26730-26730/com.example.sition.videopoc D/v_gal: [tid=26730] gralloc_unregister_buffer: ===>Width = 800, Height = 480, surface = 0x5ac75c38
04-05 15:54:02.789 26730-26730/com.example.sition.videopoc D/v_gal: [tid=26730] gralloc_unregister_buffer: ===>Width = 800, Height = 480, surface = 0x5ac44360
04-05 15:54:03.546 26730-26730/com.example.sition.videopoc W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection
04-05 15:54:06.609 26730-26730/com.example.sition.videopoc I/MediaRecorderJNI: prepare: surface=0x5ac59450 (identity=2947)
04-05 15:54:06.656 26730-26730/com.example.sition.videopoc D/v_gal: [tid=26730] gralloc_register_buffer: ===>Width = 800, Height = 480, surface = 0x5ac75c38
04-05 15:54:06.765 26730-26730/com.example.sition.videopoc D/v_gal: [tid=26730] gralloc_register_buffer: ===>Width = 800, Height = 480, surface = 0x5ac44360
04-05 15:54:23.382 26730-26766/com.example.sition.videopoc W/Camera: Camera server died!
04-05 15:54:23.382 26730-26766/com.example.sition.videopoc W/IMediaDeathNotifier: media server died
04-05 15:54:25.382 26730-26730/com.example.sition.videopoc D/v_gal: [tid=26730] gralloc_unregister_buffer: ===>Width = 800, Height = 480, surface = 0x5ac75c38
04-05 15:54:25.382 26730-26730/com.example.sition.videopoc D/v_gal: [tid=26730] gralloc_unregister_buffer: ===>Width = 800, Height = 480, surface = 0x5ac44360

这是 Activity 发生这种情况的地方:

public class MainActivity 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_main);

        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();
    }
}

这是布局文件:

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

    <SurfaceView
        android:id="@+id/cameraView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

编辑:

我什至无法打开此应用程序创建的文件(但它们确实包含数据)。

你不妨看看这里,有些东西可能会有帮助,比如设置预览显示或相机参数:

Android Camera Server Died and Camera ERROR 100