android录屏功能的实现

近期项目需要录屏功能,功能已实现,特此记录一下。

 //<----------------------------------录屏开始--------------------------------------->

 private MediaRecorder mediaRecorder;
 private VirtualDisplay virtualDisplay;
 int scDpi ;
 int scWidth;
 int scheight ;

 private void getRecordPermission() {
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
         mediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
         Intent intent = mediaProjectionManager.createScreenCaptureIntent();
         startActivityForResult(intent, REQUESTRECORD);
     }
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
         if (requestCode==REQUESTRECORD){
             MediaProjection mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
             //获取mediaRecorder
             mediaRecorder = getMediaRecorder();
             virtualDisplay = mediaProjection.createVirtualDisplay("我的工作录屏",
                     scWidth, scheight, scDpi,
                     DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
                     mediaRecorder.getSurface(),
                     null, null);
         }
     }

     //开始录制
     try {
         mediaRecorder.start();
     } catch (IllegalStateException e) {
         e.printStackTrace();
     }

 }


 private void getScreenSize() {
     DisplayMetrics dm = getResources().getDisplayMetrics();
     scDpi = dm.densityDpi;
     scWidth = dm.widthPixels;
     scheight = dm.heightPixels;
 }


 private MediaRecorder getMediaRecorder() {
     MediaRecorder mediaRecorder = new MediaRecorder();
     mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);    //音频载体
     mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);    //视频载体
     mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);   //输出格式
     mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);  //音频格式
     mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); //视频格式
     mediaRecorder.setVideoSize(scWidth, scheight);  //size
     mediaRecorder.setVideoFrameRate(30);    //帧率
     mediaRecorder.setVideoEncodingBitRate(3 * 1024 * 1024); //比特率
     mediaRecorder.setOrientationHint(0);    //旋转角度

     //创建文件夹
     File dir = Ut.getRMapsProjectPrivateTasksOutputDir(RecognizeActivity.this);
     if (!dir.exists()) {
         dir.mkdirs();
     }
     //创建文件名
     String fileName = "录像作业"+new SimpleDateFormat("yyyyMMddHHmm").format(new Date()) + ".mp4";
     //设置文件位置
     videoPath = dir + "/" + fileName;
     mediaRecorder.setOutputFile(videoPath);

     try {
         mediaRecorder.prepare();
     } catch (IllegalStateException | IOException e) {
         e.printStackTrace();
     }

     return mediaRecorder;
 }

 //<----------------------------------录屏结束--------------------------------------->
发布了4 篇原创文章 · 获赞 1 · 访问量 1120
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览