Kotlin

Inicializando a sessão

1. Realizar a inicialização da sessão

Solicite permissões para a câmera e o microfone e, em seguida, conecte-se à sessão do Vonage:

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

2. Captura de tela da solicitação “Session connected”

Quando estiver conectado, abra a caixa de diálogo de captura de tela do sistema:

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

3. Quando o usuário conceder permissão, inicie a captura

Quando o usuário compartilha a tela por meio da caixa de diálogo:

  1. Inicie o serviço em primeiro plano – Necessário antes de usar MediaProjection.
  2. Obter MediaProjection – A partir da intenção do resultado.
  3. Criar o capturadorScreenSharingCapturer(context, mediaProjection).
  4. Criar o editor – Use o capturador e defina o tipo de vídeo como PublisherKitVideoTypeScreen.
  5. Publicarsession.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 otimiza a codificação do conteúdo da tela (por exemplo, texto e interface do usuário).

Etapa 5: Serviço em primeiro plano

O ScreenSharingService exibe uma notificação e faz uma chamada startForeground() com ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION. Isso precisa acontecer antes você liga getMediaProjection().

ScreenSharingManager se vincula a este serviço e expõe startForeground(). Inicialize-o em onCreate e desvincular em onDestroy.