Callback
abstract class Callback
kotlin.Any | |
↳ | android.media.projection.MediaProjection.Callback |
Callbacks for the projection session.
Summary
Public constructors | |
---|---|
Callback() |
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 |
onCapturedContentVisibilityChanged(isVisible: Boolean) Invoked immediately after capture begins or when the visibility of the captured region changes, providing the current visibility of the captured region. |
open Unit |
onStop() Called when the MediaProjection session has been stopped and is no longer valid. |
Public constructors
Callback
Callback()
Public methods
onCapturedContentResize
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
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 valuetrue
), 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 valuefalse
) 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
open fun onStop(): Unit
Called when the MediaProjection session has been stopped and 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
). If the application is displaying any UI indicating the MediaProjection state it should be updated to indicate that MediaProjection is no longer active.
MediaProjection stopping can be a result of the system stopping the ongoing MediaProjection due to various reasons, such as another MediaProjection session starting, a user stopping the session via UI affordances in system-level UI, or the screen being locked.
After this callback any call to MediaProjection.createVirtualDisplay
will fail, even if no such VirtualDisplay
was ever created for this MediaProjection session.