繁体   English   中英

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

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

如果在录制视频时最小化应用程序 - 一切正常,但是一旦我部署应用程序,就会收到此错误:

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)

或者,如果我在 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)

如何在最小化应用程序时正确停止录制视频???

这是我的代码:我收集配置:

CameraX.unbindAll()
    getDisplayMetrics()
    setPreviewConfig()

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

设置 videoConfig 和 videoCapture:

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

    videoCapture = VideoCapture(videoCaptureConfig)

然后我开始录制视频:

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

在 onPause() 上,我得到的错误如上所述

谢谢

在 onPause 上停止视频时我遇到了同样的错误。 为了解决这个问题,我在调用super.onPause()之前添加了一个延迟(参见: android: camera onPause/onResume issue )。

  1. 声明 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. 添加点击监听器
button.setOnClickListener(v -> {
    if(!isRecording){
        videoCapture.startRecording(videoFile, CameraXExecutors.mainThreadExecutor(), videoSavedListener);
        isRecording = true;
     }else{
        videoCapture.stopRecording();
     }
});
  1. 覆盖 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();
    }

请注意,视频录制用例目前在 API 中标记为隐藏,处于非常初步的状态,可能会发生变化。

编辑:对于某些设备,在使用 videoCapture 用例集调用onPause()时,应用程序仍然会崩溃。 我添加了CameraX.unbindAll()以在调用super.onPause()之前删除所有用例。 然后,在onResume()方法中我再次绑定它们。

声明一个布尔变量

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