Outlook
Recent security update enable android 14 devices to select “A Single App” with screen capturing. In my android application, when user select “A Single App” and select my application itself on next dialog, createVirtualDisplay() makes Error “Could not create virtual display: xxxx(display name)”.
If possible, I want to know the reason and to fix it.
Code
The error come from this code. (unrelated code is omitted)
class HogehogeActivity : AppCompatActivity(), org.webrtc.VideoCapturer {
private lateinit var screenCaptureLauncher: ActivityResultLauncher<Intent>
private var surfaceTextureHelper: SurfaceTextureHelper? = null
override fun onCreate() {
screenCaptureLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode != Activity.RESULT_OK) {
return@registerForActivityResult
}
val data = it.data ?: return@registerForActivityResult
if (!Settings.canDrawOverlays(this)) return@registerForActivityResult
val manager = getSystemService(Context.MEDIA_PROJECTION_SERVICE) as? MediaProjectionManager ?: return@registerForActivityResult
val mediaProjection = manager.getMediaProjection(Activity.RESULT_OK, screenCaptureIntent) ?: return@registerForActivityResult
// Error occur: [DisplayManager] Could not create virtual display: WebRTC_ScreenCapture
val virtualDisplay = mediaProjection.createVirtualDisplay(
"WebRTC_ScreenCapture",
480,
640,
400,
VIRTUAL_DISPLAY_FLAG_PUBLIC.or(VIRTUAL_DISPLAY_FLAG_PRESENTATION),
Surface(surfaceTextureHelper?.surfaceTexture),
null,
null
)
}
// user tap button to start screen capture.
binding.button.setOnClickLisner {
val manager = (activity.getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager)
val screenCaptureIntent = manager.createScreenCaptureIntent()
screenCaptureLauncher.launch(screenCaptureIntent)
}
}
override fun initialize(
surfaceTextureHelper: SurfaceTextureHelper?,
applicationContext: Context?,
capturerObserver: CapturerObserver?
) {
this.surfaceTextureHelper = surfaceTextureHelper
}
}
I tried
createVirtualDisplay()
‘s arguments change.- select “A single app” and select other app =>
createVirtualDisplay()
succeeded. - select “Entire screen” =>
createVirtualDisplay()
succeeded.
New contributor
house is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.