Android12 录屏时代码设置不显示屏幕上悬浮窗
时间: 2023-08-06 19:03:20 浏览: 155
在 Android 12 中,录屏时默认不会显示悬浮窗,这是为了保护用户的隐私和安全。如果您需要在录屏时显示悬浮窗,可以使用以下代码:
```java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
MediaProjection mediaProjection = mediaProjectionManager.getMediaProjection(Activity.RESULT_OK, mScreenCaptureIntent);
DisplayMetrics metrics = getResources().getDisplayMetrics();
int screenWidth = metrics.widthPixels;
int screenHeight = metrics.heightPixels;
int screenDensity = metrics.densityDpi;
MediaRecorder mediaRecorder = new MediaRecorder();
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setVideoSize(screenWidth, screenHeight);
mediaRecorder.setVideoFrameRate(30);
mediaRecorder.setVideoEncodingBitRate(5 * 1024 * 1024);
mediaRecorder.setVideoEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setAudioSamplingRate(44100);
mediaRecorder.setAudioChannels(1);
mediaRecorder.setAudioEncodingBitRate(16 * 1024);
mediaRecorder.setOutputFile(getSaveDirectory() + "/" + fileName);
Surface inputSurface = mediaRecorder.createInputSurface();
Display display = getWindowManager().getDefaultDisplay();
Surface displaySurface = display.createVirtualDisplay("Recording Display", screenWidth, screenHeight, screenDensity, null, DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION, inputSurface, null);
mediaRecorder.prepare();
mediaRecorder.start();
showFloatingWindow();
}
```
其中 `showFloatingWindow()` 方法是您自己实现的显示悬浮窗的方法。注意,在 Android 12 中,您需要在应用程序的 `AndroidManifest.xml` 文件中添加 `android.permission.CAPTURE_VIDEO_OUTPUT` 权限才能录制屏幕。
阅读全文