I am trying to develop an Android app which interacts from Camera hardware to record video. It records the video for 10 seconds, saves it and sends it to server(in background thread) and starts another video for next 10 seconds.It is expected to take videos indefinitely unless user presses the “stop” button in the app. Even if user presses home button then the video recording should continue in background.
Currently, if user presses the home button then the camera preview generates an exception as
“Java: IO Exception: Invalid preview surface”
Here is the code snippet I have used
recorder.setOutputFile(outputFile+"_"+number+".mp4");
recorder.setPreviewDisplay( holder.getSurface());
if (recorder != null) {
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
}
}
Can I continue recording in the background even when user presses the home key?
I don’t have any link documenting to this behavior, but I guess it’s not possible. Even though you use a Service, it keeps throwing exceptions saying that it has “lost the surface”.
You can check this other question as well.