当屏幕关闭时,后台相机预览出错
When screen is off, camera preview in background comes to error
代码基于 GoogleSamples,并在示例代码中更改了一些内容以便在后台预览而不是拍照。
例如:
@Override
public void onResume() {
super.onResume();
/*startBackgroundThread();
// When the screen is turned off and turned back on, the SurfaceTexture is already
// available, and "onSurfaceTextureAvailable" will not be called. In that case, we can open
// a camera and start preview from here (otherwise, we wait until the surface is ready in
// the SurfaceTextureListener).
if (mTextureView.isAvailable()) {
openCamera(mTextureView.getWidth(), mTextureView.getHeight());
} else {
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
}*/
}
@Override
public void onPause() {
/*closeCamera();
stopBackgroundThread();*/
super.onPause();
}
public synchronized void start() {
if (mCameraDevice != null) {
return;
}
startBackgroundThread();
// When the screen is turned off and turned back on, the SurfaceTexture is already
// available, and "onSurfaceTextureAvailable" will not be called. In that case, we can open
// a camera and start preview from here (otherwise, we wait until the surface is ready in
// the SurfaceTextureListener).
if (mTextureView.isAvailable()) {
openCamera(mTextureView.getWidth(), mTextureView.getHeight());
} else {
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
}
}
public synchronized void stop() {
if (mCameraDevice == null) {
return;
}
closeCamera();
stopBackgroundThread();
}
但是,当屏幕关闭一分钟时,相机预览出现错误:
enter image description here
enter image description here
为什么以及如何避免?
startForegroundService(new Intent(this, EmptyService.class));
activity 需要前台服务,然后一切正常。
代码基于 GoogleSamples,并在示例代码中更改了一些内容以便在后台预览而不是拍照。
例如:
@Override
public void onResume() {
super.onResume();
/*startBackgroundThread();
// When the screen is turned off and turned back on, the SurfaceTexture is already
// available, and "onSurfaceTextureAvailable" will not be called. In that case, we can open
// a camera and start preview from here (otherwise, we wait until the surface is ready in
// the SurfaceTextureListener).
if (mTextureView.isAvailable()) {
openCamera(mTextureView.getWidth(), mTextureView.getHeight());
} else {
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
}*/
}
@Override
public void onPause() {
/*closeCamera();
stopBackgroundThread();*/
super.onPause();
}
public synchronized void start() {
if (mCameraDevice != null) {
return;
}
startBackgroundThread();
// When the screen is turned off and turned back on, the SurfaceTexture is already
// available, and "onSurfaceTextureAvailable" will not be called. In that case, we can open
// a camera and start preview from here (otherwise, we wait until the surface is ready in
// the SurfaceTextureListener).
if (mTextureView.isAvailable()) {
openCamera(mTextureView.getWidth(), mTextureView.getHeight());
} else {
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
}
}
public synchronized void stop() {
if (mCameraDevice == null) {
return;
}
closeCamera();
stopBackgroundThread();
}
但是,当屏幕关闭一分钟时,相机预览出现错误: enter image description here enter image description here
为什么以及如何避免?
startForegroundService(new Intent(this, EmptyService.class));
activity 需要前台服务,然后一切正常。