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