Skip to main content
Version: 0.x.x

Screen Share - Android

This feature enables hosts to start or stop sharing their screen with other hosts and audience members during the live stream. Only hosts (in SEND_AND_RECV mode) can broadcast their screen, while audience members (in RECV_ONLY mode) can view it in real time.

How Screen share works?​

  • The following diagram shows flow of the screen sharing in android using VideoSDK :

VideoSDK Android Screenshare Flow Diagram

enableScreenShare()​

  • By using enableScreenShare() function of Meeting class,the host can share their mobile screen to other hosts and audience members.

  • You can pass customised screenshare track in enableScreenShare() by using Custom Screen Share Track.

  • Screen Share stream of the participant can be accessed from the onStreamEnabled event of ParticipantEventListener.

Screenshare permission​

  • A participant’s Screen share stream is provided via the MediaProjection API. This API is only compatible with Build.VERSION_CODES.LOLLIPOP or higher.

  • 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.

  • One will get a prompt dialog like this:

user permission

  • After permission is received from the user, you can call enableScreenShare() method.
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) {
// Enabling screen share
liveStream!!.enableScreenShare(data)
}
}

Customise notification​

  • When a presenter starts screen share, presenter will receive a notification with a pre-defined title and message.

  • Notification with pre-defined title and message will look like this:

notification
  • You can Customise those title, message and icon as per your requirements using <meta-data> specified in app/src/main/AndroidManifest.xml.
<application>
<meta-data
android:name="notificationTitle"
android:value="@string/notificationTitle"
/>
<meta-data
android:name="notificationContent"
android:value="@string/notificationContent"
/>
<meta-data
android:name="notificationIcon"
android:resource="@mipmap/ic_launcher_round"
/>
</application>

disableScreenShare()​

  • By using disableScreenShare() function of Meeting class,the host can stop sharing their mobile screen to other hosts and audience members.
private fun disableScreenShare() {
// Disabling screen share
liveStream!!.disableScreenShare()
}

Events associated with enableScreenShare​

  • Participant who share their mobile screen will receive a callback on onStreamEnabled() of the Participant with Stream object.

  • While other Participants will receive onPresenterChanged() callback of the Meeting class with the participantId as presenterId who started the screen share.

Events associated with disableScreenShare​

  • Participant who shared their mobile screen will receive a callback on onStreamDisabled() of the Participant with Stream object.

  • While other Participants will receive onPresenterChanged() callback of the Meeting class with the presenterId as null indicating there is no presenter.

private fun setLocalListeners() {
liveStream!!.localParticipant.addEventListener(object : ParticipantEventListener() {
//Callback for when the participant starts a stream
override fun onStreamEnabled(stream: Stream) {
if (stream.kind.equals("share", ignoreCase = true)) {
Log.d("VideoSDK","Share Stream On: onStreamEnabled $stream");
}
}

//Callback for when the participant stops a stream
override fun onStreamDisabled(stream: Stream) {
if (stream.kind.equals("share", ignoreCase = true)) {
Log.d("VideoSDK","Share Stream On: onStreamDisabled $stream");
}
}
});
}

private val meetingEventListener: MeetingEventListener = object : MeetingEventListener() {
//Callback for when the presenter changes
override fun onPresenterChanged(participantId: String) {
if(!TextUtils.isEmpty(participantId))
{
Log.d("VideoSDK","$participantId started screen share");
}else{
Log.d("VideoSDK","some one stopped screen share");
}
}
}

Rendering Screen Share Stream​

  • When a host participant shares their screen, the onStreamEnabled() function of the ParticipantEventListener is triggered, allowing the Stream to be added to a VideoView.
private fun setLocalListeners() {
liveStream!!.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
shareView!!.addTrack(videoTrack)
}
}
override fun onStreamDisabled(stream: Stream) {
if (stream.kind.equals("share", ignoreCase = true)) {
shareView!!.removeTrack()
}
}
});
}

API Reference​

The API references for all the methods and events utilised in this guide are provided below.

Got a Question? Ask us on discord