Screen Share

Android SDK provides support for sharing the entire screen of the device to the room.

Please note that for a peer to share their screen, their role must have screenshare enabled in the dashboard. Also select the appropriate resolution that you desire the screenshare track should be of.


How to start screenshare from the app (without a custom notification)

To start screen share, app needs to call the startScreenshare method of HMSSDK, which takes in two parameters - The first one is HMSActionResultListener which is a callback object needed to inform about success or failure of the action The second one is a Intent which is the result data of MediaProjection permission activity; the calling app must validate that result code is Activity.RESULT_OK before calling this method.

Following is the snippet on how to use this:

// Define a result Launcher var resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> if (result.resultCode == Activity.RESULT_OK) { val mediaProjectionPermissionResultData: Intent? = // Pass this intent to hmsSDK hmsSDK.startScreenshare(object : HMSActionResultListener { override fun onError(error: HMSException) { // an error occurred } override fun onSuccess() { // started successfully } } ,mediaProjectionPermissionResultData) } } // Get the MEDIA_PROJECTION_SERVICE and launch the result launcher val mediaProjectionManager: MediaProjectionManager? = requireContext().getSystemService( Context.MEDIA_PROJECTION_SERVICE ) as MediaProjectionManager resultLauncher.launch(mediaProjectionManager?.createScreenCaptureIntent())

It is important to note that screenshare will not work if the resultCode is NOT Activity.RESULT_OK. SDK will also start a foreground service with a notification when screenshare is started, which is mandated above Android 10.

How to add a custom notification

To take a look at how this is implemented in our sample app, click here.

hmssdk.startScreenShare also takes an optional third parameter of type notification. See how to build a custom notification in the android docs here.

It could look something like this: Since this is a standard Android notification the full range of customization is available.

fun startScreenshare(mediaProjectionPermissionResultData: Intent?, actionListener: HMSActionResultListener) { // Without custom notification // hmsSDK.startScreenshare(actionListener ,mediaProjectionPermissionResultData) // With custom notification val notification = NotificationCompat.Builder(getApplication(), "ScreenCapture channel") .setContentText("Screenshare running for roomId: ${hmsRoom?.roomId}") .setSmallIcon(R.drawable.arrow_up_float) .build() hmsSDK.startScreenshare(actionListener, mediaProjectionPermissionResultData, notification) }

How to get Screen Share Status

Application needs to call the isScreenShareActive method of HMSSDK.

This method returns a Boolean which will be true inscase ScreenShare is currently active and being used, and False for inactive state.


How to stop the screenshare and dissmiss the foreground notification from the notification.

To stop the screenshare, create a custom notification and add the following action to it. The image could be anything, but the action has to be gotten from the HMSScreenCaptureService. This will create a "STOP SCREENSHARE" button on the intent.

This pending intent could be passed anywhere you'd like, including on tap of the notification itself.

val notification = NotificationCompat.Builder(getApplication(), "ScreenCapture channel") .addAction(R.drawable.ic_menu_close_clear_cancel, "Stop Screenshare", HMSScreenCaptureService.getStopScreenSharePendingIntent(getApplication())) .build()

How to stop screenshare

Application needs to call the stopScreenshare method of HMSSDK and pass an instance of HMSActionResultListener to listen to the callbacks. SDK also stops the foreground service on calling this method.

hmsSDK.stopScreenshare(object : HMSActionResultListener { override fun onError(error: HMSException) { // an error occurred } override fun onSuccess() { // stopped successfully } } )

It is advisable to call leave API from the onDestroy() of the activity/ fragment that

started the screenshare otherwise the foreground service will be running if even user kills the app from the Recents tab.

DONOT forget to add the permission for foreground service

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

Have a suggestion? Recommend changes ->

Was this helpful?