In this guide we'll use the Screen Capturer quickstart 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 instead:
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.
Starting in Android 10, developers are required to specify a foreground service when using the MediaProjection
API. Reference the following snippet from the quickstart example AndroidManifest.xml
.
1<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>23<application>4<service5android:enabled="true"6android:name=".ScreenCapturerService"7android:foregroundServiceType="mediaProjection">8</service>9</application>
1@Override2public void onCreate(Bundle savedInstanceState) {3super.onCreate(savedInstanceState);4setContentView(R.layout.activity_screen_capturer);5localVideoView = findViewById(R.id.local_video);6}
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.
1private void requestScreenCapturePermission() {2Log.d(TAG, "Requesting permission to capture screen");3MediaProjectionManager mediaProjectionManager = (MediaProjectionManager)4getSystemService(Context.MEDIA_PROJECTION_SERVICE);56// This initiates a prompt dialog for the user to confirm screen projection.7startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(),8REQUEST_MEDIA_PROJECTION);9}
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.
1private void startScreenCapture() {2screenVideoTrack = LocalVideoTrack.create(this, true, screenCapturer);3screenCaptureMenuItem.setIcon(R.drawable.ic_stop_screen_share_white_24dp);4screenCaptureMenuItem.setTitle(R.string.stop_screen_share);56localVideoView.setVisibility(View.VISIBLE);7screenVideoTrack.addRenderer(localVideoView);8}
Found an error ? Open an issue on Github
Stuck on something? Can't find what you're looking for? Don't hesitate to contact Twilio Support through the Console or Help Center and we'll happily give you a hand.