Android录屏,通过MediaProjection与MediaRecorder,最简单的实现方式

一、获取MediaProjectionManager,请求录制权限

private MediaProjectionManager mediaProjectionManager;

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

 

二、用户给与权限后返回,开始录制

private MediaRecorder mediaRecorder;

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != RESULT_OK)
        return;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        MediaProjection mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);

        //获取mediaRecorder
        mediaRecorder = getMediaRecorder();

        virtualDisplay = mediaProjection.createVirtualDisplay("你的 name",
                scWidth, scheight, scDpi,
                DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
                mediaRecorder.getSurface(),
                null, null);
    }

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

    //定时结束录制(不用rxjava的忽略)
    compositeDisposable.add(Observable.timer(10000, TimeUnit.MILLISECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(aLong -> {
                try {
                    mediaRecorder.stop();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                }
            }));
}

 

获取屏幕尺寸

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

 

MediaRecorder设置

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 = new File("你的 filepath");
    if (!dir.exists()) {
        dir.mkdirs();
    }

    //创建文件名
    String fileName = System.currentTimeMillis() / 1000 + ".mp4";

    //设置文件位置
    String filePath = dir + "/" + fileName;
    mediaRecorder.setOutputFile(filePath);

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

    return mediaRecorder;
}

 

注:这是最简单的实现,只支持录制本页面

你可能感兴趣的:(Android录屏,通过MediaProjection与MediaRecorder,最简单的实现方式)