0

I am trying to make a video recording app that can keep recording with screen off. I am using Android Services and the app will still record video if I hit the home button and enter the app again to stop recording. But once I turn the screen off the app stops recording and the media file gets corrupted. What should I do to make it work?

I have tried every possible solution on the platform and still can't figure out how to make the video to keep recording after screen is locked. But when I exit the app via home button the app works fine and keeps recording.

Here is the code which starts and stops the service from my MainActivity class:

        captureButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if(! isRecording){

                Intent intent = new Intent(MainActivity.this, CameraService.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startService(intent);

                isRecording = true;

            } else {

                Intent intent = new Intent(MainActivity.this, CameraService.class);
                stopService(intent);

                isRecording = false;

            }
        }
    });

And here is the code from the CameraService class that handles the recording:

@Override
public void onCreate() {


    super.onCreate();

    mPreview = MainActivity.mPreview;

    captureButton = MainActivity.captureButton;

    mCamera = MainActivity.mCamera;


        //initialize camera
        if(prepareVideoRecorder()){

            //Camera available and unlocked, media recorder prepared, start recording
            mediaRecorder.start();

            //inform user recording start
            captureButton.setText("Stop");

            Log.v("", "In onCreate(), isRecording is " + isRecording);

        } else{
            //prepare didn't work
            releaseMediaRecorder();
            Toast.makeText(this, "Cannot prepare video", Toast.LENGTH_LONG).show();

        }

}




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


@Override
public void onDestroy() {

    super.onDestroy();

    //stop rec and release camera
    mediaRecorder.stop();
    releaseMediaRecorder();
    mCamera.lock();

    //inform user recording has stopped
    captureButton.setText("Capture");

    Log.v("", "In onDestroy(), isRecording is " + isRecording);

}

This is what appears in the debug console right after I turn the screen off while recording:

    V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@e29724c nm : com.example.mycamera ic=null
I/InputMethodManager: [IMM] startInputInner - mService.startInputOrWindowGainedFocus
D/InputTransport: Input channel constructed: fd=73
    Input channel destroyed: fd=72
D/Graph: removeVertex() : insertDummyVertex, because there is no ancestor.
D/ViewRootImpl@750598e[MainActivity]: dispatchDetachedFromWindow
D/InputTransport: Input channel destroyed: fd=64
D/TextView: setTypeface with style : 0
D/TextView: setTypeface with style : 0
I/art: Do partial code cache collection, code=26KB, data=29KB
    After code cache collection, code=26KB, data=29KB
    Increasing code cache capacity to 128KB
D/TextView: setTypeface with style : 0
D/InputTransport: Input channel constructed: fd=68
D/ViewRootImpl@8ad5cfe[MainActivity]: setView = DecorView@e9a2c5f[MainActivity] touchMode=true
I/Choreographer: Skipped 73 frames!  The application may be doing too much work on its main thread.
D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000,  [1440x2560]-format:1
W/IInputConnectionWrapper: reportFullscreenMode on inexistent InputConnection
E/Camera: Error 2
D/InputTransport: Input channel destroyed: fd=73
W/IInputConnectionWrapper: reportFullscreenMode on inexistent InputConnection
E/ViewRootImpl: sendUserActionEvent() mView == null
I/Choreographer: Skipped 61 frames!  The application may be doing too much work on its main thread.
D/ViewRootImpl@8ad5cfe[MainActivity]: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
Lucas Tony
  • 99
  • 2
  • 8
  • Thia solution does not help me – Lucas Tony Sep 16 '19 at 22:03
  • Hello @LucasTony, where is your call to `startForeground()`? – greeble31 Sep 16 '19 at 22:16
  • Hello, right now there is no call to startForeground(), but I tried this solution as well: https://stackoverflow.com/a/36018368 adapting it to my code but in the end I had the same result unfortunately – Lucas Tony Sep 16 '19 at 22:23
  • Well you're probably going to want to stick with that, it is a required part of the solution. – greeble31 Sep 16 '19 at 22:33
  • And you might want to add a WakeLock, too; I thought that the camera would keep the device awake, but I may be mistaken. – greeble31 Sep 16 '19 at 22:36
  • Yes, I added the WakeLock as well a few hours after the original post, still nothing. I used the WakeLock and the startForeground() implementation combined but it still does not work properly – Lucas Tony Sep 16 '19 at 22:58
  • Perhaps its something to do with that error 2, which may correspond to `Camera.CAMERA_ERROR_EVICTED`. Have you added a `Camera.ErrorCallback`? What does that tell you? Also, it's not clear why you call `mCamera.lock()` in `onDestroy()`. – greeble31 Sep 16 '19 at 23:10
  • I have not added a Camera.ErrorCallback. Could you please elaborate on how to use the setErrorCallback()? Also, I removed the mCamera.lock(), put it there by mistake (still does not work) – Lucas Tony Sep 16 '19 at 23:30
  • For now, just set a trivial error callback that dumps the error code to logcat, so we can verify the camera's being evicted. You can call `setErrorCallback()` right after you open the camera. – greeble31 Sep 16 '19 at 23:36
  • Update: so I can't believe I haven't noticed this before, but if I first press the home button to go to the home screen and after that I lock the screen the app works perfectly. If I lock the screen directly from the app sometimes the recording stops and the file gets corrupted but most of the time the audio recording continues and the video just freezes. Also, the "Stop" button changes back to "Capture", but the recording seems to continue with audio. – Lucas Tony Sep 19 '19 at 16:41

0 Answers0