Skip to main content
Version: 3.x.x

Share Your Screen - Android

  • A participant can share their complete mobile screen with the other participants in a meeting.

  • This guide shows how to enable or disable screen share in a meeting.

How Screen share works?​

  • The following diagram shows the flow of screen sharing on Android with VideoSDK:

VideoSDK Android Screenshare Flow Diagram

Enable Screen Share​

  • A participant’s Screen share stream is provided via the Android MediaProjection API.

  • Get an instance of the MediaProjectionManager and call the createScreenCaptureIntent() method in an activity. This initiates a prompt dialog for the user to confirm screen projection.

  • The user sees a prompt dialog like this:

user permission

  • After permission is received from the user, call meeting.enableScreenShare() method. It is a suspend function, so call it from a coroutine. It throws VideoSDKException when the share can't start, for example 3054 when the screen is already being shared.
private fun enableScreenShare() {
val mediaProjectionManager = application.getSystemService(
MEDIA_PROJECTION_SERVICE
) as MediaProjectionManager
startActivityForResult(
mediaProjectionManager.createScreenCaptureIntent(), CAPTURE_PERMISSION_REQUEST_CODE
)
}

public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode != CAPTURE_PERMISSION_REQUEST_CODE) return
if (resultCode == RESULT_OK && data != null) {
lifecycleScope.launch {
try {
meeting.enableScreenShare(data)
} catch (e: VideoSDKException) {
Log.e("VideoSDK", "Screen share failed: ${e.code} ${e.name}")
}
}
}
}
  • enableScreenShare(data) shares the screen with the h720p_15fps encoder profile. To choose another profile, use a custom screen share track.

  • To share the device's audio along with the screen, call meeting.enableScreenShare(data, enableAudio = true). The audio is mixed into your microphone audio, so muting the mic also mutes it. This needs Android 10 (API 29) or later and the RECORD_AUDIO permission.

Screen share notification​

  • When a presenter starts screen share, the presenter receives an ongoing notification for as long as the screen is shared.

  • On Android 13 (API 33) and later, the notification shows in the notification drawer only if your app has been granted the POST_NOTIFICATIONS permission. The share works either way.

  • If the presenter removes the app from the recent apps screen during a share, the share ends.

Disable Screen Share​

By using meeting.disableScreenShare() function, a participant can stop publishing screen stream to other participants. It is a suspend function, so call it from a coroutine.

private fun disableScreenShare() {
lifecycleScope.launch {
try {
meeting.disableScreenShare()
} catch (e: VideoSDKException) {
Log.e("VideoSDK", "Stop screen share failed: ${e.code} ${e.name}")
}
}
}

Display Screen Share Stream​

Local Participant​

When the local participant shares the screen, onStreamEnabled() of ParticipantEventListener is triggered with the Stream which can be added to a VideoView.

import videosdk.org.webrtc.VideoTrack

private fun setLocalListeners() {
meeting.localParticipant.addEventListener(object : ParticipantEventListener() {
override fun onStreamEnabled(stream: Stream) {
if (stream.kind.equals("share", ignoreCase = true)) {
// display share video
val videoTrack = stream.track as? VideoTrack ?: return
shareView.addTrack(videoTrack)
}
}
override fun onStreamDisabled(stream: Stream) {
if (stream.kind.equals("share", ignoreCase = true)) {
shareView.removeTrack()
}
}
})
}

Other Participants​

When any participant starts sharing their screen, onPresenterChanged() in the MeetingEventListener is triggered with their participantId. This includes your own share; the code below skips it, because meeting.participants holds only the other participants. When the screen share stops, it is triggered with null.

import videosdk.org.webrtc.VideoTrack

private val meetingEventListener: MeetingEventListener = object : MeetingEventListener() {
override fun onPresenterChanged(participantId: String?) {
updatePresenter(participantId)
}
}

//Getting the stream from the participantId
private fun updatePresenter(participantId: String?) {
// null means nobody is sharing
if (participantId == null) return

// find participant
val participant = meeting.participants[participantId] ?: return

// find share stream in participant
var shareStream: Stream? = null
for (stream: Stream in participant.streams.values) {
if ((stream.kind == "share")) {
shareStream = stream
break
}
}
if (shareStream == null) return

// display share video
val videoTrack = shareStream.track as? VideoTrack ?: return
shareView.addTrack(videoTrack)

// listen for share stop event
participant.addEventListener(object : ParticipantEventListener() {
override fun onStreamDisabled(stream: Stream) {
if ((stream.kind == "share")) {
shareView.removeTrack()
}
}
})
}
info
  • Here screenShare stream is displayed using VideoView. The WebRTC classes, such as VideoTrack, come from the videosdk.org.webrtc package.
  • To know more about VideoView, please visit here

Got a Question? Ask us on discord