public static MyAndroidScreenRecord GetInstance(Android.App.Activity activity = null) { lock (objLock) { if (s_Instance == null) { if (activity == null) { throw new Exception("MyScreenCapture.GetInstance 单例首次创建, 请传入 activity 参数"); } s_Instance = new MyAndroidScreenRecord(activity); } return(s_Instance); } }
/// <summary> /// 开始屏幕录制 /// </summary> /// <param name="imageFileDateTime">时间参数 - 屏幕截图文件名命名规则所需</param> /// <param name="dirName">屏幕截图保存文件夹名称</param> public FileInfo StartRecord(DateTime?imageFileDateTime = null, string dirName = "") { if (s_IsRunning.Get() == true) { string msg = "正在进行录屏, 无法再多开一个"; System.Diagnostics.Debug.WriteLine(msg); showToast(msg); return(null); } s_IsRunning.Set(true); mImageFileDateTime = imageFileDateTime; mDirName = dirName; Android.Content.Intent intent = mProjectionManager.CreateScreenCaptureIntent(); mAppActivity.StartActivityForResult(intent, s_ScreenRecord_Request_Code); return(MyAndroidScreenRecord.GetScreenVideoFileInfo(mImageFileDateTime, dirName)); }
public FileInfo Get_ScreenVideoFileInfo(DateTime?imageFileDateTime = null, string dirName = "") { return(MyAndroidScreenRecord.GetScreenVideoFileInfo(imageFileDateTime, dirName)); }
private void initRecorder(int width, int height) { mMediaRecorder = new Android.Media.MediaRecorder(); // 遇到的坑 -- 用户权限没有赋予 Mic 会报错 "setAudioSource failed.", 故此处进行判断是否含有麦克风权限 if (Xamarin.Essentials.DeviceInfo.Version.Major >= 6) { Android.Content.PM.Permission permission = mAppActivity.CheckSelfPermission(Android.Manifest.Permission.RecordAudio); if (permission == Android.Content.PM.Permission.Granted) { // mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mMediaRecorder.SetAudioSource(Android.Media.AudioSource.Mic); } } // mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); mMediaRecorder.SetVideoSource(Android.Media.VideoSource.Surface); // mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mMediaRecorder.SetOutputFormat(Android.Media.OutputFormat.ThreeGpp); // mediaRecorder.setOutputFile(getsaveDirectory() + System.currentTimeMillis() + ".mp4"); // 遇到的坑 -- 目录没有创建会报出 Java.IO.FileNotFoundException, 要先判断 Dir 是否存在, 不存在则进行创建 var mp4FileInfo = MyAndroidScreenRecord.GetScreenVideoFileInfo(mImageFileDateTime, mDirName); if (mp4FileInfo.Directory.Exists == false) { mp4FileInfo.Directory.Create(); } mMediaRecorder.SetOutputFile(mp4FileInfo.FullName); // mediaRecorder.setVideoSize(width, height); mMediaRecorder.SetVideoSize(width, height); // mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); mMediaRecorder.SetVideoEncoder(Android.Media.VideoEncoder.H264); // 遇到的坑 -- 用户权限没有赋予 Mic 会报错 "setAudioSource failed.", 故此处进行判断是否含有麦克风权限 if (Xamarin.Essentials.DeviceInfo.Version.Major >= 6) { Android.Content.PM.Permission permission = mAppActivity.CheckSelfPermission(Android.Manifest.Permission.RecordAudio); if (permission == Android.Content.PM.Permission.Granted) { // mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mMediaRecorder.SetAudioEncoder(Android.Media.AudioEncoder.AmrNb); } } // mediaRecorder.setVideoEncodingBitRate(5 * 1024 * 1024); mMediaRecorder.SetVideoEncodingBitRate(5 * 1024 * 1024); // mediaRecorder.setVideoFrameRate(30); mMediaRecorder.SetVideoFrameRate(mDpi); try { mMediaRecorder.Prepare(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("MyAndroidScreenRecord - initRecorder 捕获到错误"); System.Diagnostics.Debug.WriteLine(ex.GetFullInfo()); System.Diagnostics.Debugger.Break(); throw ex; } }