Screen sharing enables a host of an interactive live streaming broadcast or a user in a video call to display what is on their screen to other users in the channel. This technology has many advantages for communicating information. For example:
To share the screen on Android, you need to use Android native APIs and Agora APIs together. The following diagram shows how data is transferred during screen sharing:
Before proceeding, ensure that you have the following:
This section shows you how to implement screen sharing. Add code in the following steps in /app/java/com.example.<projectname>/MainActivity
.
If the user shares the screen on Android 10 or later, to avoid the Android system from triggering SecurityException
(Android native callback), you need to call startForeground
(Android native method) before calling MediaProjection
to notify the user that the current device starts screen sharing.
// Start capturing screen data. Ensure that your Android version must be Lollipop or higher.
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
if(b){
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
getActivity().startForegroundService(fgServiceIntent);
}
}
}
Create an intent based on MediaProjection
, and pass the intent to the startActivityForResult
(Android native method) to start capturing screen data.
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
if(b){
...
// Instantiate a MediaProjectionManager object
MediaProjectionManager mpm = (MediaProjectionManager)
getContext().getSystemService(Context.MEDIA_PROJECTION_SERVICE);
// Create an intent
Intent intent = mpm.createScreenCaptureIntent();
// Start screen capturing
startActivityForResult(intent, PROJECTION_REQ_CODE);
}
}
Get the screen data from the activity result, and pass the screen data in the mediaProjectionPermissionResultData
parameter of startScreenCapture
to start the screen sharing.
@Override
// Listen for onActivityResult.
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PROJECTION_REQ_CODE && resultCode == RESULT_OK) {
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
ScreenCaptureParameters parameters = new ScreenCaptureParameters();
parameters.setFrameRate(DEFAULT_SHARE_FRAME_RATE);
// Start the screen sharing.
engine.startScreenCapture(data, parameters);
}
}
To send video streams showing the user's entire screen, add the following code to your project:
// Join a channel and publish the video stream from the screen sharing.
options.publishCameraTrack = false;
options.publishScreenTrack = true;
engine.joinChannel(accessToken, channelId, 0, options);
To send video streams from both the user's entire screen and local camera, add the following code to your project:
// Join a channel and publish the video stream from the local camera.
options.publishCameraTrack = true;
options.publishScreenTrack = false;
engine.joinChannel(accessToken, channelId, 0, options);
// Join the same channel and publish the video stream from the screen sharing.
ChannelMediaOptions mediaOptions = new ChannelMediaOptions();
mediaOptions.publishCameraTrack = false;
mediaOptions.publishScreenTrack = true;
String channelId = et_channel.getText().toString();
engine.joinChannelEx(null, channelId, 0, mediaOptions, iRtcEngineEventHandler, rtcConnection2);
When the user finishes the screen sharing, call stopScreenCapture
to stop the screen sharing.
// Stop the screen sharing.
engine.stopScreenCapture();
This section provides reference information you may need when sharing the screen.
Agora provides an open-source sample project that implements sharing the screen on GitHub. You can try the sample project and view the source code.
Android native APIs
Agora APIs