1

我首先尝试使用以下方法创建 SurfaceView:

SurfaceView sv = new SurfaceView(context);
// Get a surface
surfaceHolder = sv.getHolder();
// tells Android that this surface will have its data constantly
// replaced
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.TRANSLUCENT);        
wm.addView(sv, params);

然后尝试准备记录器:

try {
    camera.unlock();

    mediaRecorder = new MediaRecorder();

    mediaRecorder.setCamera(camera);
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mediaRecorder.setProfile(CamcorderProfile
                .get(CamcorderProfile.QUALITY_HIGH));

    mediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO,
                appName).toString());

    mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());

    mediaRecorder.prepare();
} catch (IllegalStateException e) {
    releaseMediaRecorder();
    Log.d(TAG,
        "IllegalStateException preparing MediaRecorder: "
                        + e.getMessage());
    return false;
} catch (IOException e) {
    releaseMediaRecorder();
    Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());

    return false;
}

return true;

但是我得到一个错误“应用程序丢失了表面”,尽管我可以看到预览正在工作。这是为什么?

4

1 回答 1

2

可能的原因是 Surface 实例只有在 MediaRecorder 实例完成后才会完全创建。

解决方案是将 SurfaceView 初始化(有问题的第一个代码片段)放在活动中的 onStart() 中。它将保证在 MediaRecorder 初始化之前完成具有所有相应回调的 Surface 创建。

于 2013-08-11T09:55:33.973 回答