Background video recording in Android 4.0

I am trying to organize background video recording in Android 4.0. But I can not do this because of these problems:

  • Dummy Surface does not work in MediaRecorder (error: invalid surface)

  • If you use Surface 1 x 1 px on Activity, Surface is destroyed when activity is paused (recording stops)

  • If you use Surface 1 x 1 px in WindowsManager, Surface is destroyed when the application is paused (recording stops)

  • SurfaceTexture does not work in MediaRecorder.setPreviewDisplay (new surface (SurfaceTexture))

  • The widget does not allow to process the surface 1 x 1 px

  • Status bar does not allow to process 1 x 1 px surface

Please help me find the right way.

+19
android background video recording
Feb 24 '13 at 5:43
source share
3 answers

I found the answer: you need to use WindowManager and call it from the Service.

-four
Feb 26 '13 at 9:22
source share

Example and simple code (tested on Bean jelly, SGS2):

public class BackgroundVideoRecorder extends Service implements SurfaceHolder.Callback { private WindowManager windowManager; private SurfaceView surfaceView; private Camera camera = null; private MediaRecorder mediaRecorder = null; @Override public void onCreate() { // Start foreground service to avoid unexpected kill Notification notification = new Notification.Builder(this) .setContentTitle("Background Video Recorder") .setContentText("") .setSmallIcon(R.drawable.ic_launcher) .build(); startForeground(1234, notification); // Create new SurfaceView, set its size to 1x1, move it to the top left corner and set this service as a callback windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); surfaceView = new SurfaceView(this); LayoutParams layoutParams = new WindowManager.LayoutParams( 1, 1, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT ); layoutParams.gravity = Gravity.LEFT | Gravity.TOP; windowManager.addView(surfaceView, layoutParams); surfaceView.getHolder().addCallback(this); } // Method called right after Surface created (initializing and starting MediaRecorder) @Override public void surfaceCreated(SurfaceHolder surfaceHolder) { camera = Camera.open(); mediaRecorder = new MediaRecorder(); camera.unlock(); mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface()); mediaRecorder.setCamera(camera); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH)); mediaRecorder.setOutputFile( Environment.getExternalStorageDirectory()+"/"+ DateFormat.format("yyyy-MM-dd_kk-mm-ss", new Date().getTime())+ ".mp4" ); try { mediaRecorder.prepare(); } catch (Exception e) {} mediaRecorder.start(); } // Stop recording and remove SurfaceView @Override public void onDestroy() { mediaRecorder.stop(); mediaRecorder.reset(); mediaRecorder.release(); camera.lock(); camera.release(); windowManager.removeView(surfaceView); } @Override public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {} @Override public void surfaceDestroyed(SurfaceHolder surfaceHolder) {} @Override public IBinder onBind(Intent intent) { return null; } } 

Do not forget about permissions:

 <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+38
May 14 '13 at 21:07
source share
 try { mediaRecorder.prepare(); } catch (Exception e) {} mediaRecorder.start(); Timer t = new Timer(); t.schedule(new TimerTask() { @Override public void run() { stopSelf(); } }, 5000); }catch(Exception e){} 

Just a small modification of the above code ... it will save the 5sec file in the root folder in ur sdcard ... change the timer according to ur need. and he worked on both the Nexus 4 and Micromax.

+2
Mar 24 '14 at 6:44
source share



All Articles