尝试创建已销毁的生命周期下的 LifecycleCamera

huangapple go评论76阅读模式
英文:

Trying to create LifecycleCamera with destroyed lifecycle

问题

I'm trying to make floating cameraview for my screen recorder application.

FloatingViewService.java

public class FloatingViewService extends LifecycleService implements CameraXConfig.Provider, LifecycleOwner {
    private View mFloatingView;
    private WindowManager mWindowManager;
    PreviewView previewView;
    private ListenableFuture<ProcessCameraProvider> cameraProviderFuture;

    public FloatingViewService() {
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        super.onBind(intent);
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mFloatingView = LayoutInflater.from(this).inflate(R.layout.layout_floating_widget, null);
        final WindowManager.LayoutParams params;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                    PixelFormat.TRANSLUCENT);

        } else {
            params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.TYPE_PHONE,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                    PixelFormat.TRANSLUCENT);

        }
        params.width = 400;
        params.height = 300;
        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        mWindowManager.addView(mFloatingView, params);

        mFloatingView.findViewById(R.id.previewFrame).setOnTouchListener(new View.OnTouchListener() {
            private int initialX;
            private int initialY;
            private float initialTouchX;
            private float initialTouchY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        initialX = params.x;
                        initialY = params.y;
                        initialTouchX = event.getRawX();
                        initialTouchY = event.getRawY();
                        return true;

                    case MotionEvent.ACTION_UP:
                        return true;

                    case MotionEvent.ACTION_MOVE:
                        //this code is helping the widget to move around the screen with fingers
                        params.x = initialX + (int) (event.getRawX() - initialTouchX);
                        params.y = initialY + (int) (event.getRawY() - initialTouchY);
                        mWindowManager.updateViewLayout(mFloatingView, params);
                        return true;
                }
                return false;
            }
        });

        cameraFun(mFloatingView);
    }

    public void cameraFun(View mFloatingView) {
        previewView = mFloatingView.findViewById(R.id.previewFrame);
        cameraProviderFuture = ProcessCameraProvider.getInstance(this);
        cameraProviderFuture.addListener(() -> {
            try {
                ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
                bindPreview(cameraProvider);
            } catch (ExecutionException | InterruptedException e) {
            }
        }, ContextCompat.getMainExecutor(this));
    }

    void bindPreview(@NonNull ProcessCameraProvider cameraProvider) {
        Preview preview = new Preview.Builder()
                .build();

        CameraSelector cameraSelector = new CameraSelector.Builder()
                .requireLensFacing(CameraSelector.LENS_FACING_FRONT)
                .build();
        preview.setSurfaceProvider(previewView.createSurfaceProvider());
        OrientationEventListener orientationEventListener = new OrientationEventListener(this) {
            @Override
            public void onOrientationChanged(int orientation) {
                int rotation;
                int orientationcheck = getResources().getConfiguration().orientation;

                if (orientationcheck == Configuration.ORIENTATION_PORTRAIT) {
                    if (orientation >= 45 && orientation < 135) {
                        rotation = 360;
                    } else if (orientation >= 135 && orientation < 225) {
                        rotation = 180;
                    } else if (orientation >= 225 && orientation < 315) {
                        rotation = 360;
                    } else {
                        rotation = 0;
                    }
                    Log.i("orientation", String.valueOf(orientation) + "= ORIENTATION_PORTRAIT" + orientationcheck);

                } else {
                    if (orientation >= 45 && orientation < 135) {
                        rotation = 90;
                    } else if (orientation >= 135 && orientation < 225) {
                        rotation = 180;
                    } else if (orientation >= 225 && orientation < 315) {
                        rotation = 270;
                    } else {
                        rotation = 0;
                    }
                    Log.i("orientation", String.valueOf(orientation) + "= ORIENTATION_Land" + orientationcheck);
                }
                Log.i("orientation", String.valueOf(orientation) + "= " + orientationcheck);

                previewView.setRotation(rotation);
            }
        };
        orientationEventListener.enable();
        cameraProvider.bindToLifecycle(this, cameraSelector, preview);
    }

    @NonNull
    @Override
    public CameraXConfig getCameraXConfig() {
        return Camera2Config.defaultConfig();
    }

    public void onDestroy() {
        super.onDestroy();
        mWindowManager.removeView(mFloatingView);
    }
}

layout_floating_widget.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.camera.view.PreviewView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/previewFrame"
    android:layout_width="300dp"
    android:layout_height="250dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"
    android:layout_gravity="center" />

BackgroundService.java

public class BackgroundService extends Service {
    Boolean onoff = true;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public void onCreate() {
        super.onCreate();
        new FloatingViewService();
        if (onoff) {
            onoff = false;
            startService(new Intent(this, FloatingViewService.class));
        } else {
            onoff = true;
            stopService(new Intent(this, FloatingViewService.class));
        }
    }
}

ERROR:

java.lang.IllegalArgumentException: Trying to create LifecycleCamera with destroyed lifecycle.
        at androidx.camera.lifecycle.LifecycleCameraRepository.createLifecycleCamera(LifecycleCameraRepository.java:103)
        at androidx.camera.lifecycle.ProcessCameraProvider.bindToLifecycle(ProcessCameraProvider.java:414)
        at androidx.camera.lifecycle.ProcessCameraProvider.bindToLifecycle(ProcessCameraProvider.java:275)
        at com.rdbrain.FloatingViewService.bindPreview(FloatingViewService.java:196)
        at com.rdbrain.FloatingViewService.lambda$cameraFun$0$FloatingViewService(FloatingViewService.java:138)
        at com.rdbrain.-$$Lambda$FloatingViewService$qkM-fNI79D-TvnilmrNFZjQYwlI.run(Unknown Source:2)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage

<details>
<summary>英文:</summary>

I&#39;m trying to make floating cameraview for my screen recorder application.  


**FloatingViewService.java**

public class FloatingViewService extends LifecycleService implements CameraXConfig.Provider, LifecycleOwner {
private View mFloatingView;
private WindowManager mWindowManager;
PreviewView previewView;
private ListenableFuture<ProcessCameraProvider> cameraProviderFuture;

public FloatingViewService() {
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
super.onBind(intent);
return null;
}
@Override
public void onCreate() {
super.onCreate();
mFloatingView = LayoutInflater.from(this).inflate(R.layout.layout_floating_widget, null);
final WindowManager.LayoutParams params;
if (android.os.Build.VERSION.SDK_INT &gt;= android.os.Build.VERSION_CODES.O) {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
} else {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
}
params.width = 400;
params.height = 300;
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mFloatingView, params);
mFloatingView.findViewById(R.id.previewFrame).setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = params.x;
initialY = params.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_UP:
return true;
case MotionEvent.ACTION_MOVE:
//this code is helping the widget to move around the screen with fingers
params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
mWindowManager.updateViewLayout(mFloatingView, params);
return true;
}
return false;
}
});
cameraFun(mFloatingView);
}
public void cameraFun(View mFloatingView) {
previewView = mFloatingView.findViewById(R.id.previewFrame);
cameraProviderFuture = ProcessCameraProvider.getInstance(this);
cameraProviderFuture.addListener(() -&gt; {
try {
ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
bindPreview(cameraProvider);
} catch (ExecutionException | InterruptedException e) {
}
}, ContextCompat.getMainExecutor(this));
}
void bindPreview(@NonNull ProcessCameraProvider cameraProvider) {
Preview preview = new Preview.Builder()
.build();
CameraSelector cameraSelector = new CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_FRONT)
.build();
preview.setSurfaceProvider(previewView.createSurfaceProvider());
OrientationEventListener orientationEventListener = new OrientationEventListener(this) {
@Override
public void onOrientationChanged(int orientation) {
int rotation;
int orientationcheck = getResources().getConfiguration().orientation;
if (orientationcheck == Configuration.ORIENTATION_PORTRAIT) {
if (orientation &gt;= 45 &amp;&amp; orientation &lt; 135) {
rotation = 360;
} else if (orientation &gt;= 135 &amp;&amp; orientation &lt; 225) {
rotation = 180;
} else if (orientation &gt;= 225 &amp;&amp; orientation &lt; 315) {
rotation = 360;
} else {
rotation = 0;
}
Log.i(&quot;orientation&quot;, String.valueOf(orientation) + &quot;= ORIENTATION_PORTRAIT&quot; + orientationcheck);
} else {
if (orientation &gt;= 45 &amp;&amp; orientation &lt; 135) {
rotation = 90;
} else if (orientation &gt;= 135 &amp;&amp; orientation &lt; 225) {
rotation = 180;
} else if (orientation &gt;= 225 &amp;&amp; orientation &lt; 315) {
rotation = 270;
} else {
rotation = 0;
}
Log.i(&quot;orientation&quot;, String.valueOf(orientation) + &quot;= ORIENTATION_Land&quot; + orientationcheck);
}
Log.i(&quot;orientation&quot;, String.valueOf(orientation) + &quot;= &quot; + orientationcheck);
previewView.setRotation(rotation);
}
};
orientationEventListener.enable();
cameraProvider.bindToLifecycle(this, cameraSelector, preview);
}
@NonNull
@Override
public CameraXConfig getCameraXConfig() {
return Camera2Config.defaultConfig();
}
public void onDestroy() {
super.onDestroy();
mWindowManager.removeView(mFloatingView);
}

}

**layout_floating_widget.xml**

<?xml version="1.0" encoding="utf-8"?>
<androidx.camera.view.PreviewView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/previewFrame"
android:layout_width="300dp"
android:layout_height="250dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_gravity="center" />


**BackgroundService.java** Check below code how I call FloatingViewService.java

public class BackgroundService extends Service {
Boolean onoff = true;

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate() {
super.onCreate();
new FloatingViewService();
if (onoff) {
onoff = false;
startService(new Intent(this, FloatingViewService.class));
} else {
onoff = true;
stopService(new Intent(this, FloatingViewService.class));
}
}

}


**ERROR:**

java.lang.IllegalArgumentException: Trying to create LifecycleCamera with destroyed lifecycle.
at androidx.camera.lifecycle.LifecycleCameraRepository.createLifecycleCamera(LifecycleCameraRepository.java:103)
at androidx.camera.lifecycle.ProcessCameraProvider.bindToLifecycle(ProcessCameraProvider.java:414)
at androidx.camera.lifecycle.ProcessCameraProvider.bindToLifecycle(ProcessCameraProvider.java:275)
at com.rdbrain.FloatingViewService.bindPreview(FloatingViewService.java:196)
at com.rdbrain.FloatingViewService.lambda$cameraFun$0$FloatingViewService(FloatingViewService.java:138)
at com.rdbrain.-$$Lambda$FloatingViewService$qkM-fNI79D-TvnilmrNFZjQYwlI.run(Unknown Source:2)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6626)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)


**Note**  : This code is working as separate module  but when I connect to screen recorder it not working, I have setup all necessary permission and service in manifests.xml
</details>
# 答案1
**得分**: 1
使用 **viewLifecycleOwner** 替代 `this` 在 `cameraProvider.bindToLifecycle(this, cameraSelector, preview);` 中。
<details>
<summary>英文:</summary>
Use **viewLifecycleOwner** instead of **this** in  `cameraProvider.bindToLifecycle(this, cameraSelector, preview);`
</details>

huangapple
  • 本文由 发表于 2020年8月6日 11:15:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63276332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定