You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android14选“单个应用”录屏时createVirtualDisplay()报错求助

问题概述

Android 14 近期的安全更新新增了录屏时选择「单个应用」的功能。在我的应用中,当用户选择「单个应用」并选中本应用时,调用createVirtualDisplay()会抛出错误:Could not create virtual display: WebRTC_ScreenCapture(显示名称为WebRTC_ScreenCapture)。

截图:选择「单个应用」

希望明确问题原因并找到修复方案。

相关代码

错误由以下代码触发(无关代码已省略):

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.setOnClickListener {
            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
    }

}
已尝试操作
  • 修改createVirtualDisplay()的参数
  • 选择「单个应用」并选中其他应用时,createVirtualDisplay()调用成功
  • 选择「全屏」时,createVirtualDisplay()调用成功

内容的提问来源于stack exchange,提问作者house

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 05:48:27