# Cast

Artifact: [androidx.media3:media3-cast](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/cast/Cast.java+class:androidx.media3.cast.Cast)

*** ** * ** ***

<br />

```
@MainThread
@UnstableApi
public class Cast
```

<br />

*** ** * ** ***

The primary entry point for interacting with the Cast context.

This singleton class manages global state associated with Cast playback, including configurations (such as the receiver application id) and resource initialization.

The singleton instance must be initialized for Cast playback to function. See `https://developer.android.com/reference/androidx/media3/cast/Cast#initialize(androidx.media3.cast.CastParams)` for details on how initialization works.

Must be called on the main process and the main thread.

## Summary

| ### Public fields |
|---|---|
| `@https://checkerframework.org/api/org/checkerframework/checker/nullness/qual/MonotonicNonNull.html https://developer.android.com/reference/java/lang/Throwable.html` | `@https://checkerframework.org/api/org/checkerframework/checker/nullness/qual/MonotonicNonNull.html https://developer.android.com/reference/androidx/media3/cast/Cast#castContextLoadFailure()` |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/media3/cast/Cast#addSessionManagerListener(com.google.android.gms.cast.framework.SessionManagerListener<com.google.android.gms.cast.framework.CastSession>)( https://developers.google.com/android/reference/com/google/android/gms/cast/framework/SessionManagerListener.html<https://developers.google.com/android/reference/com/google/android/gms/cast/framework/CastSession.html> listener )` Registers the given session manager listener. |
| `void` | `https://developer.android.com/reference/androidx/media3/cast/Cast#endCurrentSession(boolean)(boolean stopCasting)` If a Cast context is available, ends any ongoing Cast session. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/Throwable.html` | `https://developer.android.com/reference/androidx/media3/cast/Cast#getCastContextLoadFailure()()` Returns the error that caused the cast context load to fail. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developers.google.com/android/reference/com/google/android/gms/cast/framework/CastSession.html` | `https://developer.android.com/reference/androidx/media3/cast/Cast#getCurrentCastSession()()` Returns the ongoing Cast session, or null if there's no ongoing Cast session, or there's no Cast context available. |
| `static https://developer.android.com/reference/androidx/media3/cast/Cast` | `https://developer.android.com/reference/androidx/media3/cast/Cast#getSingletonInstance(android.content.Context)(https://developer.android.com/reference/android/content/Context.html context)` Returns a singleton instance of the class. |
| `void` | `https://developer.android.com/reference/androidx/media3/cast/Cast#initialize()()` Equivalent to `https://developer.android.com/reference/androidx/media3/cast/Cast#initialize(androidx.media3.cast.CastParams)`. |
| `void` | `https://developer.android.com/reference/androidx/media3/cast/Cast#initialize(androidx.media3.cast.CastParams)(https://developer.android.com/reference/androidx/media3/cast/CastParams castParams)` Initializes the singleton instance. |
| `boolean` | `https://developer.android.com/reference/androidx/media3/cast/Cast#needsInitialization()()` Returns true if initialization has not yet started. |
| `void` | `https://developer.android.com/reference/androidx/media3/cast/Cast#removeSessionManagerListener(com.google.android.gms.cast.framework.SessionManagerListener<com.google.android.gms.cast.framework.CastSession>)( https://developers.google.com/android/reference/com/google/android/gms/cast/framework/SessionManagerListener.html<https://developers.google.com/android/reference/com/google/android/gms/cast/framework/CastSession.html> listener )` Unregisters the given session manager listener. |

## Public fields

### castContextLoadFailure

```
@MonotonicNonNull
public @MonotonicNonNull Throwable castContextLoadFailure
```

## Public methods

### addSessionManagerListener

```
public void addSessionManagerListener(
    SessionManagerListener<CastSession> listener
)
```

Registers the given session manager listener.

If no Cast context is available at the time of the invocation, registration is deferred until a Cast context is available.

| Parameters |
|---|---|
| `https://developers.google.com/android/reference/com/google/android/gms/cast/framework/SessionManagerListener.html<https://developers.google.com/android/reference/com/google/android/gms/cast/framework/CastSession.html> listener` | The listener to register. |

### endCurrentSession

```
public void endCurrentSession(boolean stopCasting)
```

If a Cast context is available, ends any ongoing Cast session. Otherwise, does nothing.

| Parameters |
|---|---|
| `boolean stopCasting` | Whether to stop the receiver application. |

### getCastContextLoadFailure

```
public @Nullable Throwable getCastContextLoadFailure()
```

Returns the error that caused the cast context load to fail. Returns null if initialization has not failed or hasn't completed.

### getCurrentCastSession

```
public @Nullable CastSession getCurrentCastSession()
```

Returns the ongoing Cast session, or null if there's no ongoing Cast session, or there's no Cast context available.

### getSingletonInstance

```
public static Cast getSingletonInstance(Context context)
```

Returns a singleton instance of the class.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html context` | A `https://developer.android.com/reference/android/content/Context.html`. |

### initialize

```
public void initialize()
```

Equivalent to `https://developer.android.com/reference/androidx/media3/cast/Cast#initialize(androidx.media3.cast.CastParams)`.

### initialize

```
public void initialize(CastParams castParams)
```

Initializes the singleton instance.

This method triggers asynchronous initialization using the provided `https://developer.android.com/reference/androidx/media3/cast/CastParams`. The Cast context loading is offloaded to `https://developer.android.com/reference/androidx/media3/common/util/BackgroundExecutor`.

Calling this method after initialization has started (and possibly completed) applies the newly provided `https://developer.android.com/reference/androidx/media3/cast/CastParams`.

Initialization must occur before the creation of a `https://developer.android.com/reference/androidx/media3/cast/RemoteCastPlayer` or the use of Cast UI widgets, such as the `https://developer.android.com/reference/androidx/media3/cast/MediaRouteButtonFactory` or the `https://developer.android.com/reference/androidx/media3/cast/MediaRouteButtonKt` composable. The recommended way to do this is calling `https://developer.android.com/reference/androidx/media3/cast/Cast#initialize(androidx.media3.cast.CastParams)` in the `https://developer.android.com/reference/android/app/Application.html#onCreate()` method as follows:

```
public class MainApplication extends Application {
  &#64;Override
  public void onCreate() {
    super.onCreate();
    CastParams castParams = // Build your Cast configurations or use CastParams.DEFAULT.
    Cast.getSingletonInstance(this).initialize(castParams);
  }
}
```
Then the application can configure the `MainApplication` in the ` AndroidManifest.xml` as follows:

```
<application
    android:name=".MainApplication">
    ...
</application>
```

If the application doesn't call this method before initialization is needed (by Cast UI widgets or `https://developer.android.com/reference/androidx/media3/cast/RemoteCastPlayer`), but provides the cast options tag in the manifest, then those options are used to perform initialization without the need to call `https://developer.android.com/reference/androidx/media3/cast/Cast#initialize()` explicitly. For example:

```
<meta-data
    android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
    android:value="..." />
```

However, using manifest-provided Cast options is not recommended because they include options incompatible with Media3 such as automatic media session management, which Media3 provides built-in support for.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | if this method is not called on the main process. |

### needsInitialization

```
public boolean needsInitialization()
```

Returns true if initialization has not yet started.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/media3/cast/Cast#initialize()` |   |

### removeSessionManagerListener

```
public void removeSessionManagerListener(
    SessionManagerListener<CastSession> listener
)
```

Unregisters the given session manager listener.