close search

Add Messaging, Voice, and Authentication to your apps with Vonage Communications APIs

Visit the Vonage API Developer Portal

Back to Tutorials

Screen Sharing Tutorial (Android)


This tutorial demonstrates how to use the OpenTok Android SDK to publish a screen-sharing video.

Setting up your project

The code for this section is available in the Screen-Sharing-Java project of the opentok-android-sdk-samples repo. If you haven't already, you'll need to clone the repo into a local directory. On the command line, run:

git clone

Open the Screen-Sharing-Java project in Android Studio to follow along.

Exploring the code

The MainActivity class uses WebView object as the source for the screen-sharing video of the published stream.

In the initializePublisher() method of the MainActivity class, after creating a Publisher.Builder object the code calls the capturer() method of the Publisher.Builder object, passing in a ScreenSharingCapturer object as a parameter:

ScreenSharingCapturer screenSharingCapturer = new ScreenSharingCapturer(MainActivity.this, webViewContainer);

publisher = new Publisher.Builder(MainActivity.this)

ScreenSharingCapturer is a custom class that extends the BaseVideoCapturer class (defined in the OpenTok SDK). This class lets you define a custom video capturer to be used by an OpenTok publisher. The constructor of the ScreenSharingCapturer class is passed an Android View object, which the capturer will use as the source for the video.

The getCaptureSettings() method initializes capture settings to be used by the custom video capturer:

public CaptureSettings getCaptureSettings() {

    CaptureSettings captureSettings = new CaptureSettings();
    captureSettings.fps = fps;
    captureSettings.width = width;
    captureSettings.height = height;
    captureSettings.format = ARGB;
    return captureSettings;

The startCapture() method starts the frameProducer thread after 1/15 second:

public int startCapture() {
    capturing = true;

    handler.postDelayed(newFrame, 1000 / fps);
    return 0;

The frameProducer thread gets a Bitmap representation of the contentViewobject (the WebView), writes its pixels to a buffer, and then calls the provideIntArrayFrame() method, passing in that buffer as a parameter:

private Runnable newFrame = new Runnable() {
    public void run() {
        if (capturing) {
            int width = contentView.getWidth();
            int height = contentView.getHeight();

            if (frame == null ||
                    ScreenSharingCapturer.this.width != width ||
                    ScreenSharingCapturer.this.height != height) {

                ScreenSharingCapturer.this.width = width;
                ScreenSharingCapturer.this.height = height;

                if (bmp != null) {
                    bmp = null;

                bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

                canvas = new Canvas(bmp);
                frame = new int[width * height];
            canvas.saveLayer(0, 0, width, height, null);
            canvas.translate(-contentView.getScrollX(), - contentView.getScrollY());

            bmp.getPixels(frame, 0, width, 0, 0, width, height);

            provideIntArrayFrame(frame, ARGB, width, height, 0, false);


            handler.postDelayed(newFrame, 1000 / fps);


The provideIntArrayFrame<()/code> method, defined by the BaseVideoCapturer class sends an integer array of data to the publisher, to be used for the next video frame published.

If the publisher is still capturing video, the thread starts again after another 1/15 of a second, so that the capturer continues to supply the publisher with new video frames to publish.

Congratulations! You've finished the Screen sharing Tutorial for Android.
You can continue to play with and adjust the code you've developed here, or check out the Next Steps below. For more information on screen sharing with OpenTok, see the OpenTok Screen sharing developer guide for Android.