我是第一次尝试设置自定义SurfaceView(除外)。
onResume / onPause被调用后我得到了“媒体服务器死机”消息。
媒体服务器死后我可以添加另一个预览。知道它崩溃的原因吗?我在onPause中释放相机并在onResume上打开相机。
- 相机活动 - 公共类CameraActivity扩展了DashboardActivity {
private static String TAG = "CameraActivity";
Camera camera;
CameraPreview cameraPreview;
Context _context;
FrameLayout frameLayout;
CameraPreview preview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_layout);
this._context = this;
frameLayout = (FrameLayout) findViewById(R.id.preview);
}
@Override
protected void onResume() {
super.onResume();
camera = Camera.open();
if (null != camera) {
camera.setDisplayOrientation(90);
preview = new CameraPreview(getApplicationContext(), camera);
frameLayout.addView(preview);
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
if (camera != null) {
Log.e(TAG, "Camera was not null on back pressed!");
try {
camera.stopPreview();
} catch (Exception e) {
}
// camera.release();
// camera = null;
}
finish();
}
@Override
protected void onPause() {
super.onPause();
if (camera != null) {
camera.release();
camera = null;
}
frameLayout.removeView(preview);
preview = null;
}
- 相机预览 -
public class CameraPreview extends SurfaceView implements
SurfaceHolder.Callback {
private static final String TAG = "Preview";
SurfaceHolder mHolder;
public Camera camera;
CameraPreview(Context context, Camera camera) {
super(context);
this.camera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setFormat(PixelFormat.RGB_332);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
}
public void surfaceCreated(SurfaceHolder holder) {
try {
Camera.Parameters p = camera.getParameters();
p.setPictureSize(80, 60);
p.setColorEffect(android.hardware.Camera.Parameters.EFFECT_NONE);
p.setJpegQuality(20);
p.setPreviewFrameRate(1);
p.setPreviewFpsRange(5, 10);
p.setPreviewSize(80, 60);
camera.setParameters(p);
try {
camera.setPreviewDisplay(holder);
camera.setPreviewCallback(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera arg1) {
}
});
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void stopCameraPreview() {
}
public void takePicture() {
camera.takePicture(null, null, null);
stopCameraPreview();
}
public void surfaceDestroyed(SurfaceHolder holder) {
Log.e(TAG, "SurfaceDestroyed called");
if (camera != null) {
try {
camera.stopPreview();
} catch (Exception e) {
}
// camera.lock();
// camera.release();
// camera = null;
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Log.d(TAG, "SurfaceChanged called!");
if (holder.getSurface() == null) {
Log.d(TAG, "SurfaceChanged Surface is null! Stopping!");
return;
}
try {
Camera.Parameters parameters = camera.getParameters();
// parameters.setPreviewSize(80, 60);
camera.setParameters(parameters);
// camera.setPreviewDisplay(holder);
camera.startPreview();
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
停止相机的正确方法是
public void onPause() {
super.onPause();
releaseCamera();
}
和
public void releaseCamera() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}
}