@MainThread
@UnstableApi
public class Cast


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 initialize for details on how initialization works.

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

Summary

Public methods

void

Registers the given session manager listener.

void
endCurrentSession(boolean stopCasting)

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

@Nullable Throwable

Returns the error that caused the cast context load to fail.

@Nullable CastSession

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

static Cast

Returns a singleton instance of the class.

void

Equivalent to initialize(CastParams.DEFAULT).

void
initialize(CastParams castParams)

Initializes the singleton instance.

boolean

Returns true if initialization has not yet started.

void

Unregisters the given session manager listener.

Public fields

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
SessionManagerListener<CastSession> 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
Context context

A Context.

initialize

public void initialize()

Equivalent to initialize(CastParams.DEFAULT).

initialize

public void initialize(CastParams castParams)

Initializes the singleton instance.

This method triggers asynchronous initialization using the provided CastParams. The Cast context loading is offloaded to BackgroundExecutor.

Calling this method after initialization has started (and possibly completed) applies the newly provided Cast parameters.

Initialization must occur before the creation of a RemoteCastPlayer or the use of Cast UI widgets, such as the MediaRouteButtonFactory or the MediaRouteButtonKt composable. The recommended way to do this is calling initialize in the 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 RemoteCastPlayer), but provides the cast options tag in the manifest, then those options are used to perform initialization without the need to call 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
java.lang.IllegalStateException

if this method is not called on the main process.

needsInitialization

public boolean needsInitialization()

Returns true if initialization has not yet started.

See also
initialize

removeSessionManagerListener

public void removeSessionManagerListener(
    SessionManagerListener<CastSession> listener
)

Unregisters the given session manager listener.