Skip to contentSkip to navigationSkip to topbar
On this page

Screen Capture - Android 5.x


(warning)

Warning

This documentation is for reference only. We are no longer onboarding new customers to Programmable Video. Existing customers can continue to use the product until December 5, 2026(link takes you to an external page).

We recommend migrating your application to the API provided by our preferred video partner, Zoom. We've prepared this migration guide(link takes you to an external page) to assist you in minimizing any service disruption.

In this guide we'll use the Screen Capturer quickstart(link takes you to an external page) to demonstrate how to share your Android device application screen with other participants connected to a Room using the MediaProjection API.

If you are interested in capturing from an Android View in the View hierarchy instead of capturing from the device screen you can take a look at our custom video capturer example(link takes you to an external page) instead:


Using the Screen Capturer API

using-the-screen-capturer-api page anchor

The ScreenCapturer(link takes you to an external page) class that ships with the Video Android SDK is used to provide video frames for a LocalVideoTrack(link takes you to an external page) from a device's screen. The frames are provided via the MediaProjection API. This capturer is only compatible with Build.VERSION_CODES.LOLLIPOP or higher.

Setup your app Manifest

setup-your-app-manifest page anchor

Starting in Android 10, developers are required to specify a foreground service(link takes you to an external page) when using the MediaProjection API. Reference the following snippet from the quickstart example AndroidManifest.xml.

1
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
2
3
<application>
4
<service
5
android:enabled="true"
6
android:name=".ScreenCapturerService"
7
android:foregroundServiceType="mediaProjection">
8
</service>
9
</application>
1
@Override
2
public void onCreate(Bundle savedInstanceState) {
3
super.onCreate(savedInstanceState);
4
setContentView(R.layout.activity_screen_capturer);
5
localVideoView = findViewById(R.id.local_video);
6
}

Request screen capture permission from user

request-screen-capture-permission-from-user page anchor

Get an instance of the MediaProjectionManager(link takes you to an external page) service. Call the createScreenCaptureIntent method in a new activity. This initiates a prompt dialog for the user to confirm screen projection.

Request Screen Capturer Permission

request-screen-capturer-permission page anchor
1
private void requestScreenCapturePermission() {
2
Log.d(TAG, "Requesting permission to capture screen");
3
MediaProjectionManager mediaProjectionManager = (MediaProjectionManager)
4
getSystemService(Context.MEDIA_PROJECTION_SERVICE);
5
6
// This initiates a prompt dialog for the user to confirm screen projection.
7
startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(),
8
REQUEST_MEDIA_PROJECTION);
9
}

Start Screen capture when permission received

start-screen-capture-when-permission-received page anchor

Initialize the ScreenCapturer class, after permission is received from the user. Create the LocalVideoTrack(link takes you to an external page) object and pass the ScreenCapturer object to pass the captured local video frames. Set the VideoView object to Visible . Call the addRender method on the LocalVideoTrack object. Pass the VideoView object to begin receiving the screen capture video.

Using the Screen Capturer Class

using-the-screen-capturer-class page anchor
1
private void startScreenCapture() {
2
screenVideoTrack = LocalVideoTrack.create(this, true, screenCapturer);
3
screenCaptureMenuItem.setIcon(R.drawable.ic_stop_screen_share_white_24dp);
4
screenCaptureMenuItem.setTitle(R.string.stop_screen_share);
5
6
localVideoView.setVisibility(View.VISIBLE);
7
screenVideoTrack.addRenderer(localVideoView);
8
}

Found an error ? Open an issue on Github(link takes you to an external page)

Stuck on something? Can't find what you're looking for? Don't hesitate to contact Twilio Support through the Console(link takes you to an external page) or Help Center(link takes you to an external page) and we'll happily give you a hand.

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.