Android code example
Overview
This guide demonstrates how to use the 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:
Open the Screen-Sharing-Java project in Android Studio to follow along.
Important: Please note that your App ID is your API key.
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 is a custom class that extends the BaseVideoCapturer class (defined in the Vonage Video SDK). This class lets you define a custom video capturer to be used by a Vonage Video 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:
The startCapture() method starts the frameProducer thread after 1/15 second:
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:
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.