@MainThread
@UnstableApi
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 functions

Unit

Registers the given session manager listener.

Unit
endCurrentSession(stopCasting: Boolean)

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

CastSession?

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

java-static Cast!

Returns a singleton instance of the class.

Unit

Initializes the singleton instance.

Boolean

Returns true if initialization has not yet started.

Unit

Unregisters the given session manager listener.

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: SessionManagerListener<CastSession!>!

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: Boolean

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: Context!

A Context.

initialize

fun initialize(): Unit

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

fun needsInitialization(): Boolean

Returns true if initialization has not yet started.

See also
initialize

removeSessionManagerListener

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

Unregisters the given session manager listener.

Public properties