I am launching a service to reuse the screen using the MediaRecorder and MediaProjection functions in Android 5.1, I think Method 1 code will result in Application Not Responding error, since it works in the main thread.
I am testing the code of Method 1 , so that for a long time to recode the screen, the "Application that does not respond to the error" does not appear, why? Does this mean that the MediaRecorder and MediaProjection functions worked in a separate thread?
In Method 2 code, I create a thread to run mRecordHelper.StartRecord (mRecordArg, resultCode, mIntent); but I get java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare(), why?
Thank you for your help.
Call code
MPublicPar.RecordArg mRecordArg =new MPublicPar().new RecordArg(mContext); Intent intent = new Intent(mContext,bll.RecordService.class); intent.putExtra("resultCode",resultCode); intent.putExtra("dataIntent",data); intent.putExtra("mRecordArg",mRecordArg); startService(intent);
Method 1
public class RecordService extends Service { private RecordHelper mRecordHelper; private Context mContext; @Override public void onCreate(){ mContext=this; mRecordHelper=new RecordHelper(mContext); } @Override public void onDestroy(){ mRecordHelper.StopRecord(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { final int resultCode=intent.getIntExtra("resultCode",0); final Intent mIntent=(Intent)intent.getParcelableExtra("dataIntent"); final MPublicPar.RecordArg mRecordArg=(MPublicPar.RecordArg)intent.getSerializableExtra("mRecordArg"); mRecordHelper.StartRecord(mRecordArg,resultCode,mIntent); return super.onStartCommand(intent, flags, startId); } }
Method 2
public class RecordService extends Service { private RecordHelper mRecordHelper; private Context mContext; @Override public void onCreate(){ mContext=this; mRecordHelper=new RecordHelper(mContext); } @Override public void onDestroy(){ mRecordHelper.StopRecord(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { final int resultCode=intent.getIntExtra("resultCode",0); final Intent mIntent=(Intent)intent.getParcelableExtra("dataIntent"); final MPublicPar.RecordArg mRecordArg=(MPublicPar.RecordArg)intent.getSerializableExtra("mRecordArg"); new Thread(new Runnable() { public void run() { mRecordHelper.StartRecord(mRecordArg,resultCode,mIntent); } }).start(); return super.onStartCommand(intent, flags, startId); } }
RecordHelper.cs
public class RecordHelper { private MediaRecorder mMediaRecorder; private MediaProjection mMediaProjection; private VirtualDisplay mVirtualDisplay; private MediaProjectionManager mProjectionManager; private Context mContext; private Toast mToastText; public RecordHelper(Context mContext){ this.mContext=mContext; mProjectionManager = (MediaProjectionManager) mContext.getSystemService(Context.MEDIA_PROJECTION_SERVICE); mMediaRecorder = new MediaRecorder(); } public void StartRecord(RecordArg mRecordArg, int resultCode, Intent data){ initRecorder(mRecordArg); prepareRecorder(); mMediaProjection = mProjectionManager.getMediaProjection(resultCode, data); MediaProjectionCallback mMediaProjectionCallback = new MediaProjectionCallback(); mMediaProjection.registerCallback(mMediaProjectionCallback, null); mVirtualDisplay=createVirtualDisplay(mRecordArg); DelayStartRecord(mRecordArg); } public void StopRecord(){ try { mMediaRecorder.stop(); mMediaRecorder.reset(); mVirtualDisplay.release(); mMediaRecorder.release(); mMediaProjection.stop(); mMediaProjection = null; }catch (Exception e){ Utility.LogError("StopRecord Error " + e.getMessage() + " " + e.toString()); } } private void DelayStartRecord(RecordArg mRecordArg){ mMediaRecorder.start(); } private void initRecorder(RecordArg mRecordArg) { mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mMediaRecorder.setVideoEncodingBitRate(512 * 1000); mMediaRecorder.setVideoFrameRate(30); mMediaRecorder.setVideoSize(mRecordArg.screenWidth, mRecordArg.screenHeight); mMediaRecorder.setOutputFile(mRecordArg.videoFilename); } private void prepareRecorder() { try { mMediaRecorder.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); Utility.LogError(e.getMessage()); } catch (IOException e) { e.printStackTrace(); Utility.LogError(e.getMessage()); } } private VirtualDisplay createVirtualDisplay(RecordArg mRecordArg) { return mMediaProjection.createVirtualDisplay("ScreenRecord", mRecordArg.screenWidth, mRecordArg.screenHeight, mRecordArg.mScreenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mMediaRecorder.getSurface(), null , null ); }