MediaProjection.Callback

public static abstract class MediaProjection.Callback
extends Object

java.lang.Object
   ↳ android.media.projection.MediaProjection.Callback


Callbacks for the projection session.

Summary

Public constructors

Callback()

Public methods

void onCapturedContentResize(int width, int height)

Invoked immediately after capture begins or when the size of the captured region changes, providing the accurate sizing for the streamed capture.

void onCapturedContentVisibilityChanged(boolean isVisible)

Invoked immediately after capture begins or when the visibility of the captured region changes, providing the current visibility of the captured region.

void onStop()

Called when the MediaProjection session is no longer valid.

Inherited methods

Public constructors

Callback

public Callback ()

Public methods

onCapturedContentResize

Added in API level 34
public void onCapturedContentResize (int width, 
                int height)

Invoked immediately after capture begins or when the size of the captured region changes, providing the accurate sizing for the streamed capture.

The given width and height, in pixels, corresponds to the same width and height that would be returned from WindowMetrics.getBounds() of the captured region.

If the recorded content has a different aspect ratio from either the VirtualDisplay or output Surface, the captured stream has letterboxing (black bars) around the recorded content. The application can avoid the letterboxing around the recorded content by updating the size of both the VirtualDisplay and output Surface:

 @Override
 public String onCapturedContentResize(int width, int height) {
     // VirtualDisplay instance from MediaProjection#createVirtualDisplay
     virtualDisplay.resize(width, height, dpi);

     // Create a new Surface with the updated size (depending on the application's use
     // case, this may be through different APIs - see Surface documentation for
     // options).
     int texName; // the OpenGL texture object name
     SurfaceTexture surfaceTexture = new SurfaceTexture(texName);
     surfaceTexture.setDefaultBufferSize(width, height);
     Surface surface = new Surface(surfaceTexture);

     // Ensure the VirtualDisplay has the updated Surface to send the capture to.
     virtualDisplay.setSurface(surface);
 }

Parameters
width int

height int

onCapturedContentVisibilityChanged

Added in API level 34
public void onCapturedContentVisibilityChanged (boolean isVisible)

Invoked immediately after capture begins or when the visibility of the captured region changes, providing the current visibility of the captured region.

Applications can take advantage of this callback by showing or hiding the captured content from the output Surface, based on if the captured region is currently visible to the user.

For example, if the user elected to capture a single app (from the activity shown from MediaProjectionManager#createScreenCaptureIntent()), the following scenarios trigger the callback:

  • The captured region is visible (isVisible with value true), because the captured app is at least partially visible. This may happen if the user moves the covering app to show at least some portion of the captured app (e.g. the user has multiple apps visible in a multi-window mode such as split screen).
  • The captured region is invisible (isVisible with value false) if it is entirely hidden. This may happen if another app entirely covers the captured app, or the user navigates away from the captured app.

Parameters
isVisible boolean

onStop

Added in API level 21
public void onStop ()

Called when the MediaProjection session is no longer valid.

Once a MediaProjection has been stopped, it's up to the application to release any resources it may be holding (e.g. releasing the VirtualDisplay and Surface).