我首先尝试使用以下方法创建 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;
但是我得到一个错误“应用程序丢失了表面”,尽管我可以看到预览正在工作。这是为什么?