# MediaProjection.Callback

Added in[API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Ctors](https://developer.android.com/reference/android/media/projection/MediaProjection.Callback#pubctors)\|[Methods](https://developer.android.com/reference/android/media/projection/MediaProjection.Callback#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/media/projection/MediaProjection.Callback#inhmethods)  

# MediaProjection.Callback

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/media/projection/MediaProjection.Callback "View this page in Kotlin")\|Java

`
public
static

abstract
class
MediaProjection.Callback
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


`

|---|---------------------------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.media.projection.MediaProjection.Callback |

<br />

*** ** * ** ***

Callbacks for the projection session.

## Summary

|                                                   ### Public constructors                                                   ||
|--------------------------------------------------------------------------------------------------------------------------|---|
| ` `[Callback](https://developer.android.com/reference/android/media/projection/MediaProjection.Callback#Callback())`() ` |

|                                                                                                                                                                           ### Public methods                                                                                                                                                                           ||
|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` void` | ` `[onCapturedContentResize](https://developer.android.com/reference/android/media/projection/MediaProjection.Callback#onCapturedContentResize(int,%20int))`(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](https://developer.android.com/reference/android/media/projection/MediaProjection.Callback#onCapturedContentVisibilityChanged(boolean))`(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](https://developer.android.com/reference/android/media/projection/MediaProjection.Callback#onStop())`() ` Called when the MediaProjection session has been stopped and is no longer valid.                                                                                                                                                         |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||

## Public constructors

### Callback

```
public Callback ()
```

<br />

<br />

## Public methods

### onCapturedContentResize

Added in[API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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()](https://developer.android.com/reference/android/view/WindowMetrics#getBounds())of the captured region.

If the recorded content has a different aspect ratio from either the[VirtualDisplay](https://developer.android.com/reference/android/hardware/display/VirtualDisplay)or output[Surface](https://developer.android.com/reference/android/view/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](https://developer.android.com/reference/android/hardware/display/VirtualDisplay)and output[Surface](https://developer.android.com/reference/android/view/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);
 }
```

<br />

<br />

|       Parameters       ||
|----------|--------------|
| `width`  | `int` <br /> |
| `height` | `int` <br /> |

### onCapturedContentVisibilityChanged

Added in[API level 34](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
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](https://developer.android.com/reference/android/view/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()](https://developer.android.com/reference/android/media/projection/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.

<br />

<br />

|          Parameters           ||
|-------------|------------------|
| `isVisible` | `boolean` <br /> |

### onStop

Added in[API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void onStop ()
```

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](https://developer.android.com/reference/android/hardware/display/VirtualDisplay)and[Surface](https://developer.android.com/reference/android/view/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](https://developer.android.com/reference/android/media/projection/MediaProjection#createVirtualDisplay(java.lang.String,%20int,%20int,%20int,%20int,%20android.view.Surface,%20android.hardware.display.VirtualDisplay.Callback,%20android.os.Handler))will fail, even if no such[VirtualDisplay](https://developer.android.com/reference/android/hardware/display/VirtualDisplay)was ever created for this MediaProjection session.

<br />