@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. The recommended approach is to call initialize within onCreate. If the initialization method is called but an OptionsProvider is configured in the app's manifest, RemoteCastPlayer and UI elements will trigger initialization automatically .

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

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()

Initializes the singleton instance.

Does nothing if initialization is not needed.

Cast context loading is offloaded to BackgroundExecutor.

Cast configurations are determined by the manifest-configured options provider in the app's manifest.

Initialization must occur before the creation of a RemoteCastPlayer or the use of Cast UI widgets, such as the MediaRouteButtonFactory or the MediaRouteButtonKt composable. To achieve this, the application can perform initialization in the onCreate method as follows:

public class MainApplication extends Application {
  
  public void onCreate() {
    super.onCreate();
    Cast.getSingletonInstance(this).initialize();
  }
}
Then the application can configure the MainApplication in the AndroidManifest.xml as follows:
<application
    android:name=".MainApplication">
    ...
</application>
Throws
java.lang.NullPointerException

if the singleton instance is not created via getSingletonInstance.

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.