简体   繁体   English

CameraX - 录制视频时在 onPause() 上崩溃应用程序

[英]CameraX - crash app on onPause() while recording video

if minimize app while recording video - everything all right, but once I deploy the application, ерут getting this error:如果在录制视频时最小化应用程序 - 一切正常,但是一旦我部署应用程序,就会收到此错误:

E/AndroidRuntime: FATAL EXCEPTION: CameraX-video encoding thread
Process: <pkgname>, PID: 12340
java.lang.IllegalStateException
    at android.media.MediaCodec.native_dequeueOutputBuffer(Native Method)
    at android.media.MediaCodec.dequeueOutputBuffer(MediaCodec.java:2698)
    at androidx.camera.core.VideoCapture.videoEncode(VideoCapture.java:604)
    at androidx.camera.core.VideoCapture$2.run(VideoCapture.java:348)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:214)
    at android.os.HandlerThread.run(HandlerThread.java:65)

Or if I stopped recording on onPause videoCapture?.stopRecording() , then getting this error:或者,如果我在 onPause videoCapture?.stopRecording()上停止录制,则会出现此错误:

E/AndroidRuntime: FATAL EXCEPTION: CameraX-
Process: <pkgname>, PID: 9489
java.lang.IllegalStateException
    at androidx.core.util.Preconditions.checkState(Preconditions.java:96)
    at androidx.core.util.Preconditions.checkState(Preconditions.java:108)
    at androidx.camera.camera2.impl.Camera.openCaptureSession(Camera.java:874)
    at androidx.camera.camera2.impl.Camera.onUseCaseReset(Camera.java:625)
    at androidx.camera.camera2.impl.Camera$11.run(Camera.java:611)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:214)
    at android.os.HandlerThread.run(HandlerThread.java:65)

How right stop record video while minimize app???如何在最小化应用程序时正确停止录制视频???

here's my code: I collect configurations:这是我的代码:我收集配置:

CameraX.unbindAll()
    getDisplayMetrics()
    setPreviewConfig()

    when (typeCapture) {
        TYPE_IMAGE -> {
            setImageCapture()
            CameraX.bindToLifecycle(this, preview, imageCapture)
        }
        TYPE_VIDEO -> {
            setVideoCapture()
            CameraX.bindToLifecycle(this, preview, videoCapture)
        }
    }

set videoConfig and videoCapture:设置 videoConfig 和 videoCapture:

val videoCaptureConfig = VideoCaptureConfig.Builder().apply {
        setLensFacing(lensFacing)
        setTargetAspectRatioCustom(screenAspectRatio)
        setTargetRotation(rotation)
    }.build()

    videoCapture = VideoCapture(videoCaptureConfig)

then I start recording video:然后我开始录制视频:

videoCapture?.startRecording(videoFile, 
CameraXExecutors.mainThreadExecutor(), recordListener)

on onPause() the errors I get are described above在 onPause() 上,我得到的错误如上所述

Thanks谢谢

I had the same error when stoping the video on onPause.在 onPause 上停止视频时我遇到了同样的错误。 To solve it I added a delay before to call super.onPause() ( see: android: camera onPause/onResume issue ).为了解决这个问题,我在调用super.onPause()之前添加了一个延迟(参见: android: camera onPause/onResume issue )。

  1. Declare videoSavedListener声明 videoSavedListener
private VideoCapture.OnVideoSavedListener videoSavedListener= new VideoCapture.OnVideoSavedListener() {
    @Override
    public void onVideoSaved(@NonNull File file) {
        if(isRecording) {
            isRecording = false;
            // Do whatever you want
        }
    }

    @Override
    public void onError(@NonNull VideoCapture.VideoCaptureError videoCaptureError, @NonNull String message, @Nullable Throwable cause) {

    }
};
  1. Add onClickListener添加点击监听器
button.setOnClickListener(v -> {
    if(!isRecording){
        videoCapture.startRecording(videoFile, CameraXExecutors.mainThreadExecutor(), videoSavedListener);
        isRecording = true;
     }else{
        videoCapture.stopRecording();
     }
});
  1. Override onPause()覆盖 onPause()
    @SuppressLint("RestrictedApi")
    @Override
    public void onPause() {
        if(isRecording){
            isRecording = false;
            videoCapture.stopRecording();

            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            super.onPause();
        }else
            super.onPause();
    }

Please note that video recording use case is currently marked as hidden in the API and is in a very preliminary state and subject to change.请注意,视频录制用例目前在 API 中标记为隐藏,处于非常初步的状态,可能会发生变化。

EDIT: With some devices the app still crash when calling onPause() with the videoCapture use case set.编辑:对于某些设备,在使用 videoCapture 用例集调用onPause()时,应用程序仍然会崩溃。 I added CameraX.unbindAll() to remove all the use cases before calling super.onPause() .我添加了CameraX.unbindAll()以在调用super.onPause()之前删除所有用例。 Then, in the onResume() method I bind them again.然后,在onResume()方法中我再次绑定它们。

Declare a boolean variable声明一个布尔变量

public class MyClass{
private boolean isSafe;
private boolean isPending
onPause{
isSafe=false;
}
onPostResume{
isSafe=true;
if(isPending)
methodForDoingUrAction();
}
methodForDoingUrAction(){

if(isSAfe){
//do your process
isPending=false;
}
else
isPending=true;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM