簡體   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