CastOptions.Builder

  • CastOptions.Builder is used to create a CastOptions instance for initializing CastContext.

  • The builder provides various methods to configure CastOptions, including setting media options, enabling reconnection service, specifying launch options, and controlling session transfer behavior.

  • You can set the receiver application ID to discover and launch a specific receiver device and application.

  • The builder allows you to control whether sessions are resumed unexpectedly dropped or if the receiver application is terminated when a session ends.

  • Supported namespaces can be set to filter discovered receiver devices.

public static final class CastOptions.Builder extends Object

A builder to create an instance of CastOptions which is used to initialize the CastContext.

Public Constructor Summary

Public Method Summary

CastOptions
build()
Builds and returns the CastOptions object.
CastOptions.Builder
setCastMediaOptions(CastMediaOptions castMediaOptions)
Sets the CastMediaOptions that will be used to configure the media session.
CastOptions.Builder
setEnableReconnectionService(boolean enableReconnectionService)
Sets whether ReconnectionService should be enabled when needed to better handle session recovery.
CastOptions.Builder
setLaunchOptions(LaunchOptions launchOptions)
Sets the receiver launch options to use when launching the application.
CastOptions.Builder
setMediaTransferRestrictedToSelfProviders(boolean mediaTransferRestrictedToSelfProviders)
Sets whether the Media Router's MediaTransferReceiver feature is restricted to its own MediaRouteProviderService provider services that handle the action MediaRoute2ProviderService declared by the app.
CastOptions.Builder
setReceiverApplicationId(String applicationId)
Sets the receiver application ID.
CastOptions.Builder
setRemoteToLocalEnabled(boolean remoteToLocalEnabled)
Sets whether the sender application supports transferring a cast session from a cast device to a local device.
CastOptions.Builder
setResumeSavedSession(boolean resumeSavedSession)
Sets whether the saved session should be resumed if it was dropped unexpectedly.
CastOptions.Builder
setSessionTransferEnabled(boolean sessionTransferEnabled)
Sets whether the sender application should display cast devices (in addition to the phone speaker and bluetooth devices) on the output switcher when a MediaTransferReceiver exists in the AndroidManifest.xml.
CastOptions.Builder
setShowSystemOutputSwitcherOnCastIconClick(boolean showOutputSwitcherOnCastIconClick)
Sets whether the sender application uses output switcher as the media router dialog on Android T+ (SDK 33 and above), and does nothing on lower versions.
CastOptions.Builder
setStopReceiverApplicationWhenEndingSession(boolean stopApp)
Specifies whether the receiver application should be terminated when a session is ended by the user via the "Stop Casting" button in the media router dialog.
CastOptions.Builder
setSupportedNamespaces(List<String> supportedNamespaces)
Sets the list of supported namespaces.
CastOptions.Builder
setVolumeDeltaBeforeIceCreamSandwich(double volumeDelta)
This method is deprecated. Google Play services discontinued updates for Jelly Bean (API levels 16, 17 & 18) since July 2021. This method will be removed in the next release.

Inherited Method Summary

Public Constructors

public Builder ()

Public Methods

public CastOptions build ()

Builds and returns the CastOptions object.

public CastOptions.Builder setCastMediaOptions (CastMediaOptions castMediaOptions)

Sets the CastMediaOptions that will be used to configure the media session. If apps don't set a CastMediaOptions or set a null CastMediaOptions, then the Cast SDK wouldn't create a media session for the app.

Parameters
castMediaOptions the CastMediaOptions

public CastOptions.Builder setEnableReconnectionService (boolean enableReconnectionService)

Sets whether ReconnectionService should be enabled when needed to better handle session recovery. The default value is true.

Parameters
enableReconnectionService true if the ReconnectionService should be enabled, false if it should not be enabled.

public CastOptions.Builder setLaunchOptions (LaunchOptions launchOptions)

Sets the receiver launch options to use when launching the application. The default value is a default LaunchOptions.

Parameters
launchOptions The LaunchOptions.

public CastOptions.Builder setMediaTransferRestrictedToSelfProviders (boolean mediaTransferRestrictedToSelfProviders)

Sets whether the Media Router's MediaTransferReceiver feature is restricted to its own MediaRouteProviderService provider services that handle the action MediaRoute2ProviderService declared by the app.

public CastOptions.Builder setReceiverApplicationId (String applicationId)

Sets the receiver application ID. The default value is an empty string. Specifying null or an empty string indicates that the app does not want to discover receiver devices or launch a receiver application.

Parameters
applicationId The receiver application ID that will be used to discover the receiver device and launch the receiver application.

public CastOptions.Builder setRemoteToLocalEnabled (boolean remoteToLocalEnabled)

Sets whether the sender application supports transferring a cast session from a cast device to a local device.

Parameters
remoteToLocalEnabled true if the sender application supports the switching from a cast session to a local device

public CastOptions.Builder setResumeSavedSession (boolean resumeSavedSession)

Sets whether the saved session should be resumed if it was dropped unexpectedly. The default value is true.

Parameters
resumeSavedSession true if the saved session should be resumed, false if the saved session should not be resumed.

public CastOptions.Builder setSessionTransferEnabled (boolean sessionTransferEnabled)

Sets whether the sender application should display cast devices (in addition to the phone speaker and bluetooth devices) on the output switcher when a MediaTransferReceiver exists in the AndroidManifest.xml. If this method is not called, the default value is true.

Parameters
sessionTransferEnabled false if the output switcher should only show the bluetooth devices and phone even though the AndroidManifest.xml has a MediaTransferReceiver; true to display cast devices on the output switcher.

public CastOptions.Builder setShowSystemOutputSwitcherOnCastIconClick (boolean showOutputSwitcherOnCastIconClick)

Sets whether the sender application uses output switcher as the media router dialog on Android T+ (SDK 33 and above), and does nothing on lower versions.

If this is set to true, clicking the cast icon will attempt to display the system output switcher instead of the in-app MediaRouter device picker UI. Note that the system output switcher is only shown when it is supported by the Android system. If the Android system cannot provide a system output switcher at the time of the request, the application will automatically fall back to the standard in-app MediaRouter device picker UI.

The output switcher is a system UI dialog where users can switch the output between different devices like the built-in speaker, bluetooth devices, and cast devices. It also allows volume control, group playback, and pairing new bluetooth devices. On Android versions that support it, the output switcher is the recommended affordance to present for the user to make routing changes.

Important: You must follow these instructions in order to integrate with the output switcher correctly; just setting this flag is not sufficient. Make sure you pay particular attention to the instructions for adding a MediaTransferReceiver entry to your manifest, and making sure your SessionManagerListener is prepared to receive callbacks even when you don't have an activity running in the foreground.

This flag may be dynamically changed after the CastContext is initialized by calling CastContext.setShowSystemOutputSwitcherOnCastIconClick(boolean).

Parameters
showOutputSwitcherOnCastIconClick true if the system output switcher should be shown when the cast icon is clicked on Android T+ (SDK 33 and above).

public CastOptions.Builder setStopReceiverApplicationWhenEndingSession (boolean stopApp)

Specifies whether the receiver application should be terminated when a session is ended by the user via the "Stop Casting" button in the media router dialog. The default value is false.

Parameters
stopApp true if the receiver application should be terminated when a session ended by the user, false if the receiver application should not be terminated in this case.

public CastOptions.Builder setSupportedNamespaces (List<String> supportedNamespaces)

Sets the list of supported namespaces. The default value is an empty list.

Parameters
supportedNamespaces The list of supported namespaces that is used to filter discovered receiver devices.

public CastOptions.Builder setVolumeDeltaBeforeIceCreamSandwich (double volumeDelta)

This method is deprecated.
Google Play services discontinued updates for Jelly Bean (API levels 16, 17 & 18) since July 2021. This method will be removed in the next release.

Sets the amount of receiver device volume to increase or decrease when the physical volume key is pressed on an Android device older than ICE CREAM SANDWICH. This value is not used on Android devices running ICE CREAM SANDWICH or newer. The system honors the system volume range of [0.0, 1.0]. If volumeDelta is set to 0.1, then every press of the "volume up" key will increase the volume by 10%.

Parameters
volumeDelta The amount of volume delta. The default value is 0.05.
Throws
IllegalArgumentException If volumeDelta is negative, zero or greater than 0.5.