# 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
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/kotlin/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 functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/cast/Cast#addSessionManagerListener(com.google.android.gms.cast.framework.SessionManagerListener<com.google.android.gms.cast.framework.CastSession>)( listener: 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!>! )` Registers the given session manager listener. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/cast/Cast#endCurrentSession(boolean)(stopCasting: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)` If a Cast context is available, ends any ongoing Cast session. |
| `https://developers.google.com/android/reference/com/google/android/gms/cast/framework/CastSession.html?` | `https://developer.android.com/reference/kotlin/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. |
| `java-static https://developer.android.com/reference/kotlin/androidx/media3/cast/Cast!` | `https://developer.android.com/reference/kotlin/androidx/media3/cast/Cast#getSingletonInstance(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html!)` Returns a singleton instance of the class. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/cast/Cast#initialize()()` Equivalent to `https://developer.android.com/reference/kotlin/androidx/media3/cast/Cast#initialize(androidx.media3.cast.CastParams)`. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/cast/Cast#initialize(androidx.media3.cast.CastParams)(castParams: https://developer.android.com/reference/kotlin/androidx/media3/cast/CastParams!)` Initializes the singleton instance. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/cast/Cast#needsInitialization()()` Returns true if initialization has not yet started. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/cast/Cast#removeSessionManagerListener(com.google.android.gms.cast.framework.SessionManagerListener<com.google.android.gms.cast.framework.CastSession>)( listener: 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!>! )` Unregisters the given session manager listener. |

| ### Public properties |
|---|---|
| `@https://checkerframework.org/api/org/checkerframework/checker/nullness/qual/MonotonicNonNull.html https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-throwable/index.html!` | `@https://checkerframework.org/api/org/checkerframework/checker/nullness/qual/MonotonicNonNull.html https://developer.android.com/reference/kotlin/androidx/media3/cast/Cast#castContextLoadFailure()` |

## Public functions

### addSessionManagerListener

```
fun addSessionManagerListener(
    listener: SessionManagerListener<CastSession!>!
): Unit
```

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 |
|---|---|
| `listener: 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!>!` | The listener to register. |

### endCurrentSession

```
fun endCurrentSession(stopCasting: Boolean): Unit
```

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

| Parameters |
|---|---|
| `stopCasting: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | Whether to stop the receiver application. |

### getCurrentCastSession

```
fun getCurrentCastSession(): CastSession?
```

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

### getSingletonInstance

```
java-static fun getSingletonInstance(context: Context!): Cast!
```

Returns a singleton instance of the class.

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

### initialize

```
fun initialize(): Unit
```

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

### initialize

```
fun initialize(castParams: CastParams!): Unit
```

Initializes the singleton instance.

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

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

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

```kotlin
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:

```kotlin
<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/kotlin/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/kotlin/androidx/media3/cast/Cast#initialize()` explicitly. For example:

```kotlin
<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

```
fun needsInitialization(): Boolean
```

Returns true if initialization has not yet started.

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

### removeSessionManagerListener

```
fun removeSessionManagerListener(
    listener: SessionManagerListener<CastSession!>!
): Unit
```

Unregisters the given session manager listener.

## Public properties

### castContextLoadFailure

```
@MonotonicNonNull
val castContextLoadFailure: @MonotonicNonNull Throwable!
```