Rxjava 2 exception with camera

∥☆過路亽.° 提交于 2019-12-05 05:46:52

The code you wrote for releasing the camera is prone to have race-conditions. A small change which could already make a difference is setting the flag before the action happens.

cameraReleased = true;
camera.release();
camera = null;

It might be necessary to recheck the flag just before the release of the Camera, but for this you synchronized the code before. In this context the issue is, that you're calling synchronized(this) within the deferred Observable. Instead you should synchronize on the same instance like everywhere else, because this doesn't refer to the outer class. Instead use this@Cameras

@Override
public ObservableSource<?> call() throws Exception {
    if (LogDog.isEnabled) LogDog.e("Debug::"+TAG + "::stopPreview()::AsyncTask::doInBackground()", " (camera != null) =" + (camera != null) );
    synchronized (this@Cameras) {
        if ( (null != camera) && (!cameraReleased) ) {
            if (LogDog.isEnabled)  LogDog.e("Debug::" + TAG + "::stopPreview()::AsyncTask::doInBackground()", " XXX CALL camera.stopPreview()");
            camera.stopPreview();
        }
    }
    return Completable.complete().toObservable();
}

Additional to this, you're use-case for Observable.defer() doesn't look right. The Completable.fromAction() factory might be more suitable.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!