I want to record APP for screen recording, there are two ways: RecordHelper_Method_A and RecordHelper_Method_B.
In RecordHelper_Method_A, I define mMediaRecorder, MediaProjection mMediaProjection and mVirtualDisplay as a static var, it's easy to call it like StartRecord( mContext, requestCode, resultCode,data) , StopRecord() .
and in RecordHelper_Method_B ββI need to define mMediaRecorder, MediaProjection mMediaProjection in the main class Activity and pass parameters when calling StartRecord(mMediaRecorder, mMediaProjection,mVirtualDisplay) , 'StopRecord (mMediaRecorder, mMediaject` a bit difficult ...).
I donβt know which one is better, and I donβt know anymore whether this static var can be released correctly in RecordHelper_Method_A when I finish APP.
By the way, if you have a better way, could you tell me? Thanks!
RecordHelper_Method_A
public class RecordHelper_Method_A { private static MediaRecorder mMediaRecorder; private static MediaProjection mMediaProjection; private static VirtualDisplay mVirtualDisplay; public static void StartRecord(Context mContext,int requestCode, int resultCode, Intent data){ mMediaRecorder = new MediaRecorder(); initRecorder(); MediaProjectionManager mProjectionManager = (MediaProjectionManager) mContext.getSystemService(Context.MEDIA_PROJECTION_SERVICE); mMediaProjection = mProjectionManager.getMediaProjection(resultCode, data); mVirtualDisplay=mMediaProjection.createVirtualDisplay("MainActivity", 400,600, 300, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mMediaRecorder.getSurface(), null, null); MediaProjectionCallback mMediaProjectionCallback = new MediaProjectionCallback(); mMediaProjection.registerCallback(mMediaProjectionCallback, null); mMediaRecorder.start(); } public static void StopRecord(){ mMediaProjection=null; mMediaRecorder.stop(); mMediaRecorder.reset(); mMediaRecorder.release(); mVirtualDisplay.release(); } private static void initRecorder() { mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); //... } private static class MediaProjectionCallback extends MediaProjection.Callback { @Override public void onStop() { mMediaRecorder.stop(); } } }
RecordHelper_Method_B
public class RecordHelper_Method_B { public static void StartRecord(MediaRecorder mMediaRecorder,MediaProjection mMediaProjection,VirtualDisplay mVirtualDisplay){ initRecorder(mMediaRecorder); mVirtualDisplay=mMediaProjection.createVirtualDisplay("MainActivity", 400,600, 300, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mMediaRecorder.getSurface(), null, null); MediaProjectionCallback mMediaProjectionCallback = new MediaProjectionCallback(mMediaRecorder); mMediaProjection.registerCallback(mMediaProjectionCallback, null); mMediaRecorder.start(); } public static void StopRecord(MediaRecorder mMediaRecorder,MediaProjection mMediaProjection,VirtualDisplay mVirtualDisplay){ mMediaProjection=null; mMediaRecorder.stop(); mMediaRecorder.reset(); mMediaRecorder.release(); mVirtualDisplay.release(); } private static void initRecorder(MediaRecorder mMediaRecorder) { mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); //... } private static class MediaProjectionCallback extends MediaProjection.Callback { MediaRecorder mMediaRecorder; public MediaProjectionCallback(MediaRecorder mMediaRecorder){ this.mMediaRecorder=mMediaRecorder; } @Override public void onStop() { mMediaRecorder.stop(); } } }