This guide was created as a two part guide. In Part 1 we showed you how you can implement calls with video functionality. In this Part 2 we will be showing you how to implement a simple screen sharing functionality.
In the first part, we added all the permissions we would need to be able to make a successful video call. Now in Part 2 we need to modify the original manifest with an additional permission. The FOREGROUND_SERVICE permission allows for screen share.
Next, down in the application tag <application>…</application>, add the following to your Manifest for defining a foreground service:
Now that we have added the appropriate permissions/services to the manifest, let’s modify the activity_main.xml layout we created in part 1. For this we will want to add a button to allow for sharing our screen.
After you have added the button, your xml will look like the above.
Now we need to modify our implementation. First go to your MainActivity.java. In the list of global variables we created in Part 1 for both Buttons, and Video Views, add another Button for Share Screen.
Then add a global variable for requesting permission to do screen recording (set any number higher than zero)
Now in our initElementsOnScreen Method you will want to initialize the butShareScreen and set a onClickListener.
To share your screen, Android uses the Media Projector Manager. You need to ask for a new permission to your user to do this.
As mentioned before, if your targetSdkVersion is 29 or higher, you will need to implement a Foreground Service (as per Android request). For the sake of simplicity, we will use version 28.
When you click the Share Screen button on screen, the method startScreenShare()will be executed.
This function will ask for MediaProjectionManager permissions to the user.
Once the user accepts, onActivityResult(…) will be invoked automatically:
Our final call is to the startScreenShare(…) method from Sendbird Calls. This will do all the work for us.
To stop sharing your screen you just need to invoke the stopScreenShare method from our global mCurrentCall.
Make sure you first check for a valid object. The CompletionHandler handler will be called once the screen share process is done. Continue with your code with or without errors. For the sake of this guide, we opted to not add a UI element for ending the screen sharing in lieu of showing the implementation for it.
Now we have fully completed this tutorial. In Part 1 we implemented the functionality for making calls, and now in part 2 we have added the functionality for sharing your screen.
Please feel free to check out the below resources for the full source code, and related documentation!
Check the full source, clone and download.
Also check our official documentation and sample code.