Created
January 25, 2017 10:03
-
-
Save joseph-zhong/c2a52a5507d6588bec2db9869c860114 to your computer and use it in GitHub Desktop.
Background Videorecording in Android: http://stackoverflow.com/questions/15049041/background-video-recording-in-android-4-0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment