グループ通話画面共有
グループ通話(カンファレンス)で画面共有を実装するサンプルコードです。
ここでは、MediaProjection APIを使用してサービスで画面共有機能を実装する方法を説明します。この実装は、アプリがバックグラウンドにある場合でも画面共有を維持できるようにサポートします。
前提条件
開始する前に、次の作業が必要です。
必須権限の追加
AndroidManifest.xml
に次の権限を含めてください。
<!-- For Foreground Service -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<!-- For Screen Capture on Android 14+ -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION" />
フォアグラウンドサービスの登録
アプリがバックグラウンド状態でも画面共有を維持できるように、mediaProjection
フォアグラウンドサービスのタイプを指定してサービスを宣言してください。
<service
android:name=".MyScreenCaptureService"
android:foregroundServiceType="mediaProjection" />
Tip
フォアグラウンドサービスに関する詳細については、Android公式ドキュメント(Foreground services、Foreground service types are required)を参照してください。
送信側 - 画面キャプチャーサービスを実装する
このサービスは、MediaProjectionを使用して画面キャプチャーの開始および中止作業を処理し、グループ通話と連携します。
class MyScreenCaptureService : Service() {
companion object {
private const val SCREEN_CAPTURE_ID = "Screen capture"
// This can be any dummy number
private const val NOTIFICATION_ID = 315
private const val ACTION_START_SCREEN_CAPTURE = "ACTION_START_SCREEN_CAPTURE"
private const val ACTION_STOP_SCREEN_CAPTURE = "ACTION_STOP_SCREEN_CAPTURE"
private lateinit var mediaProjectionResult:ActivityResult
private var subgroupName: String? = null
@Synchronized
@JvmStatic
fun startScreenCapturing(context: Context, projectionPermissionResult: ActivityResult, destSubgroupName:String? = null) {
val intent = Intent(context, MyScreenCaptureService::class.java).apply {
action = ACTION_START_SCREEN_CAPTURE
mediaProjectionResult = projectionPermissionResult
subgroupName = destSubgroupName
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent)
} else {
context.startService(intent)
}
}
@Synchronized
@JvmStatic
fun stopScreenCapturing(context: Context) {
val intent = Intent(context, MyScreenCaptureService::class.java).apply {
action = ACTION_STOP_SCREEN_CAPTURE
}
context.startService(intent)
}
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val action = intent?.action ?: return START_NOT_STICKY
when (action) {
ACTION_START_SCREEN_CAPTURE -> {
onScreenShareStarted()
}
ACTION_STOP_SCREEN_CAPTURE -> {
onScreenShareStopped()
}
}
return START_NOT_STICKY
}
private fun onScreenShareStarted() {
val notification = createNotification()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startForeground(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION)
} else {
startForeground(NOTIFICATION_ID, notification)
}
mediaProjectionResult.data?.let { data ->
val screenSource = ScreenCapturerVideoSource.getInstance(mediaProjectionResult.resultCode, data)
screenSource.setOnErrorListener { onScreenShareStopped() }
PlanetKit.getConference()?.let {
it.startMyScreenShare(screenSource, subgroupName) { response ->
if (!response.isSuccessful) {
onScreenShareStopped()
}
}
}
}
}
private fun onScreenShareStopped() {
PlanetKit.getConference()?.let {
it.stopMyScreenShare()
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
stopForeground(STOP_FOREGROUND_REMOVE)
}
stopSelf()
}
private fun createNotification(): Notification {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
SCREEN_CAPTURE_ID,
SCREEN_CAPTURE_ID,
NotificationManager.IMPORTANCE_NONE
)
// Register the channel with the system
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
return NotificationCompat.Builder(
this,
SCREEN_CAPTURE_ID
)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle("Screen share in progress")
.setPriority(NotificationCompat.PRIORITY_LOW)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.build()
}
}
送信側 - アクティビティで画面キャプチャーを管理する
このアクティビティは、画面キャプチャーの権限をリクエストし、サービスを開始または中止する方法を示しています。
class MyConferenceActivity : AppCompatActivity() {
private var myScreenShareSubgroupName: String? = null
private val requestMediaProjectionActivityResultLauncher: ActivityResultLauncher<Intent> =
registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { projectionPermissionResult: ActivityResult ->
val data = projectionPermissionResult.data
if (projectionPermissionResult.resultCode != RESULT_OK || data == null) {
Log.e(TAG, "Could not start screen capture!")
return@registerForActivityResult
}
MyScreenCaptureService.startScreenCapturing(context, permissionResult, myScreenShareSubgroupName)
}
private fun requestMyScreenCapture(subgroupName: String?) {
myScreenShareSubgroupName = subgroupName
val mediaProjectionManager =
getSystemService(MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
// This initiates a prompt dialog for the user to confirm screen projection.
requestMediaProjectionActivityResultLauncher.launch(mediaProjectionManager.createScreenCaptureIntent())
}
fun stopMyScreenCapture(context: Context) {
MyScreenCaptureService.stopScreenCapturing(context)
}
}
送信側 - 画面共有の目的地を変更する
画面共有の目的地は目的地のタイプによって、次のとおりで変更できます。
- メインルーム(main room)ではない、他のサブグループに目的地を変更するには、サブグループ名を引数として
changeMyScreenShareDestination()
を呼び出します。 - メインルームに目的地を変更するには、
changeMyScreenShareDestinationToMainRoom()
を呼び出します。
// Change the destination to a subgroup other than the main room
conference?.changeMyScreenShareDestination(subgroupName) {
Log.d("Conference", "changeMyScreenShareDestination onResponse: $it")
if(it.isSuccessful) {
...
}
else {
...
}
}
// Change the destination to the main room
conference?.changeMyScreenShareDestinationToMainRoom() {
Log.d("Conference", "changeMyScreenShareDestinationToMainRoom onResponse: $it")
if(it.isSuccessful) {
...
}
else {
...
}
}
受信側 - 画面共有のアップデートイベントを受信する
まずは、PeerControlListener
のonScreenShareUpdated
イベントを通じて、ピアの画面共有が開始または中止されたかを確認します。
class PeerContainer internal constructor(
peer: PlanetKitConferencePeer
) {
val peerControl: PlanetKitPeerControl = peer.createPeerControl()
?: throw IllegalStateException("Failed to create peer control.")
init {
val peerControlListener = object : PlanetKitPeerControl.PeerControlListener {
...
override fun onScreenShareUpdated(peer: PlanetKitConferencePeer, state: PlanetKitScreenShareState, subgroupName: String?) {
Log.d("Conference", "onScreenShareUpdated: user=(${peer.user}), screenShareState=$screenShareState, subgroup=$subgroupName")
}
}
peerControl.register(peerControlListener)
}
}
受信側 - ピアの画面共有ビデオを開始する
ピアの画面共有ビデオを開始するには、PlanetKitPeerControl
のstartScreenShare()
を呼び出します。
var peer: PlanetKitUser
var videoView: PlanetKitVideoView
conference.addPeerScreenShareView(peer, videoView)
videoView.resetFirstFrameRendered()
peerControl?.startScreenShare(subgroupName, { response ->
Log.d("Conference", "isSuccessful=${response.isSuccessful}")
})
受信側 - ピアの画面共有ビデオを中止する
ピアの画面共有ビデオを中止するには、PlanetKitPeerControl
のstopScreenShare()
を呼び出します。
peerControl?.stopScreenShare(null, { response ->
Log.d("Conference", "isSuccessful=${response.isSuccessful}")
})
conference?.removePeerScreenShareView(peer, videoView)