Menu

Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Screen Capture

In this guide we'll show you 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 instead:

Using the Screen Capturer API

The ScreenCapturer class that ships with the Video Android SDK is used to provide video frames for a LocalVideoTrack 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.

Initialize a Video View
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_capturer);
        localVideoView = findViewById(R.id.local_video);
    }
Request screen capture permission from user

Get an instance of the MediaProjectionManager 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
    private void requestScreenCapturePermission() {
        Log.d(TAG, "Requesting permission to capture screen");
        MediaProjectionManager mediaProjectionManager = (MediaProjectionManager)
                getSystemService(Context.MEDIA_PROJECTION_SERVICE);

        // This initiates a prompt dialog for the user to confirm screen projection.
        startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(),
                REQUEST_MEDIA_PROJECTION);
    }
Start Screen capture when permission received

Initialize the ScreenCapturer class, after permission is received from the user. Create the LocalVideoTrack 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
    private void startScreenCapture() {
        screenVideoTrack = LocalVideoTrack.create(this, true, screenCapturer);
        screenCaptureMenuItem.setIcon(R.drawable.ic_stop_screen_share_white_24dp);
        screenCaptureMenuItem.setTitle(R.string.stop_screen_share);

        localVideoView.setVisibility(View.VISIBLE);
        screenVideoTrack.addRenderer(localVideoView);
    }

Found an error ? Open an issue on Github

Stuck on something? Can't find what you're looking for? Don't hesitate to reach out to us at help@twilio.com and we'll happily give you a hand.

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

Loading Code Sample...