Kotlin

セッションの初期化

1.セッションの初期化を行う

カメラ/マイクの使用許可をリクエストし、Vonageセッションに接続します:

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

2.セッション接続リクエスト画面のキャプチャ

接続したら、システムの画面キャプチャダイアログを起動します:

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

3.ユーザーが許可したら、キャプチャを開始する。

ユーザーがダイアログを介して画面を共有する場合:

  1. フォアグラウンド・サービスの開始 - 使用前に必要 MediaProjection.
  2. 取得 MediaProjection - 結果の意図から
  3. キャプチャーの作成 - ScreenSharingCapturer(context, mediaProjection).
  4. パブリッシャーの構築 - キャプチャーを使用し、ビデオタイプを PublisherKitVideoTypeScreen.
  5. 出版 - 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 は、スクリーンコンテンツ(テキストやUIなど)のエンコーディングを最適化します。

ステップ5:フォアグラウンド・サービス

について ScreenSharingService 通知を表示し startForeground()ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION.これは必ず起こる 以前 お電話 getMediaProjection().

ScreenSharingManager このサービスにバインドし startForeground().で初期化する。 onCreate でバインドを解除する。 onDestroy.