Android11 屏幕录制时代码去掉悬浮窗的录制
时间: 2023-03-03 19:47:54 浏览: 329
在Android 11中进行屏幕录制时,可以通过以下代码片段去除悬浮窗的录制:
```java
MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
Intent permissionIntent = mediaProjectionManager.createScreenCaptureIntent();
startActivityForResult(permissionIntent, REQUEST_CODE_SCREEN_CAPTURE);
MediaRecorder mediaRecorder = new MediaRecorder();
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setVideoSize(DISPLAY_WIDTH, DISPLAY_HEIGHT);
mediaRecorder.setVideoFrameRate(30);
mediaRecorder.setOutputFile(Environment.getExternalStorageDirectory() + "/video.mp4");
try {
mediaRecorder.prepare();
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
VirtualDisplay virtualDisplay = mediaProjection.createVirtualDisplay("ScreenCapture",
DISPLAY_WIDTH, DISPLAY_HEIGHT, screenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mediaRecorder.getSurface(), null, null);
mediaRecorder.start();
```
具体来说,需要将 `VirtualDisplay` 的 `flags` 参数设置为 `VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR`,这样可以在录制时隐藏悬浮窗。
阅读全文