Added in API level 21

Callback

abstract class Callback
kotlin.Any
   ↳ android.media.projection.MediaProjection.Callback

Callbacks for the projection session.

Summary

Public constructors

Public methods
open Unit
onCapturedContentResize(width: Int, height: Int)

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

open Unit

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

open Unit

Called when the MediaProjection session is no longer valid.

Public constructors

Callback

Callback()

Public methods

onCapturedContentResize

Added in API level 34
open fun onCapturedContentResize(
    width: Int,
    height: Int
): Unit

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 android.view.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);
  }

onCapturedContentVisibilityChanged

Added in API level 34
open fun onCapturedContentVisibilityChanged(isVisible: Boolean): Unit

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.

onStop

Added in API level 21
open fun onStop(): Unit

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).