Kotlin

Iniciar sesión

1. Inicialización de la sesión

Solicita permisos de cámara/micrófono y, a continuación, conéctate a la sesión de Vonage:

private fun initializeSession(appId: String, sessionId: String, token: String) {
    session = Session.Builder(this, appId, sessionId).build().apply {
        setSessionListener(sessionListener)
        connect(token)
    }
}

2. En Sesión conectada solicitar captura de pantalla

Cuando esté conectado, inicie el cuadro de diálogo de captura de pantalla del sistema:

private fun requestScreenCapturePermission() {
    mediaProjectionManager = getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
    mediaProjectionManager?.createScreenCaptureIntent()?.let { intent ->
        screenCaptureLauncher.launch(intent)
    }
}

3. Cuando el usuario conceda el permiso, inicie la captura

Cuando el usuario comparte pantalla a través del diálogo:

  1. Iniciar el servicio en primer plano - Obligatorio antes de utilizar MediaProjection.
  2. Visite MediaProjection - De la intención de resultado.
  3. Crear el captador - ScreenSharingCapturer(context, mediaProjection).
  4. Construir el editor - Utiliza la capturadora y ajusta el tipo de vídeo a PublisherKitVideoTypeScreen.
  5. Publique - session.publish(publisher).
private fun startScreenCapture(resultCode: Int, data: Intent) {
    screenSharingManager.startForeground()

    Handler(Looper.getMainLooper()).postDelayed({
        val projectionManager = mediaProjectionManager ?: return@postDelayed
        mediaProjection = projectionManager.getMediaProjection(resultCode, data)
        val capturer = ScreenSharingCapturer(this, mediaProjection!!)

        publisher = Publisher.Builder(this)
            .capturer(capturer)
            .build()
            .apply {
                setPublisherListener(publisherListener)
                setPublisherVideoType(PublisherKit.PublisherKitVideoType.PublisherKitVideoTypeScreen)
                setStyle(BaseVideoRenderer.STYLE_VIDEO_SCALE, BaseVideoRenderer.STYLE_VIDEO_FILL)
            }

        publisherView = publisher?.view
        session?.publish(publisher)
    }, 100)
}

PublisherKitVideoTypeScreen optimiza la codificación del contenido de la pantalla (por ejemplo, texto e interfaz de usuario).

Paso 5: Servicio de primer plano

En ScreenSharingService muestra una notificación y llama a startForeground() con ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION. Esto debe suceder antes de usted llama getMediaProjection().

ScreenSharingManager se vincula a este servicio y expone startForeground(). Inicialícelo en onCreate y desvincular en onDestroy.