CastSession

public classCastSessionextendsSession

An implementation ofSession for managing connections to a Cast receiver device. An instance ofCastSession is automatically created by theSessionManager when the user selects a Cast device from the media route controller dialog. The current activeCastSession can be accessed by SessionManager.getCurrentCastSession().

See Also

Public Method Summary

void
addCastListener(Cast.Listener listener)
Adds theCast.Listener for Cast events, such as change of the application status and change of device volume.
int
getActiveInputState()
Returns the device's active-input state.
Cast.ApplicationConnectionResult
getApplicationConnectionResult()
Returns the Cast.ApplicationConnectionResult,the result of launching or joining the receiver application when starting or resuming this session.
ApplicationMetadata
getApplicationMetadata()
Returns the metadata for the currently running receiver application, or nullif the metadata is unavailable.
String
getApplicationStatus()
Returns the current receiver application status, if any.
CastDevice
getCastDevice()
Returns theCastDevice thisCastSession is casting to.
RemoteMediaClient
getRemoteMediaClient()
Returns the RemoteMediaClientfor remote media control.
long
getSessionRemainingTimeMs()
Returns the amount of time that thisCastSession instance should still be considered live by the SDK.
int
getStandbyState()
Returns the device's standby state.
double
getVolume()
Returns the device's volume, in the range [0.0, 1.0].
boolean
isMute()
Returns the device's mute state.
void
removeCastListener(Cast.Listener listener)
Removes theCast.Listener.
void
removeMessageReceivedCallbacks(Stringnamespace)
Removes the Cast.MessageReceivedCallbackfrom this session for a given namespace.
void
requestStatus()
Requests the latest status from the device.
PendingResult<Status>
sendMessage(Stringnamespace, String message)
Sends a message to the currently connected application.
void
setMessageReceivedCallbacks(Stringnamespace, Cast.MessageReceivedCallback callbacks)
Sets the new Cast.MessageReceivedCallbacklistener on this session for a given namespace.
void
setMute(boolean mute)
Mutes or unmutes the device's audio.
void
setVolume(double volume)
Sets the device volume.

Protected Method Summary

void
end(boolean stopCasting)
Ends theCastSession.
void
onResuming(Bundle routeInfoExtra)
This method is called prior to resume(Bundle).
void
onStarting(Bundle routeInfoExtra)
This method is called prior to start(Bundle).
void
resume(Bundle routeInfoExtra)
Resumes theCastSession.
void
start(Bundle routeInfoExtra)
Starts theCastSession.

Inherited Method Summary

Public Methods

public voidaddCastListener(Cast.Listener listener)

Adds theCast.Listener for Cast events, such as change of the application status and change of device volume.

Throws
IllegalStateException If this method is not called on the main thread.

public intgetActiveInputState()

Returns the device's active-input state. The returned value isCast.ACTIVE_INPUT_STATE_YES, Cast.ACTIVE_INPUT_STATE_NO, orCast.ACTIVE_INPUT_STATE_UNKNOWN.

Throws
IllegalStateException If there is no active service connection.
IllegalStateException If this method is not called on the main thread.

public Cast.ApplicationConnectionResultgetApplicationConnectionResult()

Returns theCast.ApplicationConnectionResult, the result of launching or joining the receiver application when starting or resuming this session. Returnsnullif the session is not launched or joined yet.

Throws
IllegalStateException If this method is not called on the main thread.

public ApplicationMetadata getApplicationMetadata()

Returns the metadata for the currently running receiver application, or nullif the metadata is unavailable.

Throws
IllegalStateException If there is no active service connection.
IllegalStateException If this method is not called on the main thread.

publicStringgetApplicationStatus()

Returns the current receiver application status, if any. Message text is localized to the Google Cast device's locale.

Throws
IllegalStateException If there is no active service connection.
IllegalStateException If this method is not called on the main thread.

public CastDevicegetCastDevice()

Returns theCastDevice thisCastSession is casting to.

Throws
IllegalStateException If this method is not called on the main thread.

publicRemoteMediaClient getRemoteMediaClient()

Returns the RemoteMediaClientfor remote media control.

Throws
IllegalStateException If this method is not called on the main thread.

public longgetSessionRemainingTimeMs()

Returns the amount of time that thisCastSession instance should still be considered live by the SDK. This method should be called only by the SDK.

Throws
IllegalStateException If this method is not called on the main thread.

public intgetStandbyState()

Returns the device's standby state. The returned value isCast.STANDBY_STATE_UNKNOWN, Cast.STANDBY_STATE_NO, orCast.STANDBY_STATE_YES.

Throws
IllegalStateException If there is no active service connection.
IllegalStateException If this method is not called on the main thread.

public doublegetVolume()

Returns the device's volume, in the range [0.0, 1.0].

Throws
IllegalStateException If there is no active service connection.
IllegalStateException If this method is not called on the main thread.

public booleanisMute()

Returns the device's mute state.

Throws
IllegalStateException If there is no active service connection.
IllegalStateException If this method is not called on the main thread.

public voidremoveCastListener(Cast.Listener listener)

Removes theCast.Listener.

Throws
IllegalStateException If this method is not called on the main thread.

public voidremoveMessageReceivedCallbacks(String namespace)

Removes theCast.MessageReceivedCallback from this session for a given namespace.

Parameters
namespace The namespace of the Cast channel. Namespaces must begin with the prefix " urn:x-cast:".
Throws
IOException If an I/O error occurs while performing the request.
IllegalArgumentException Ifnamespaceisnullor empty.
IllegalStateException If this method is not called on the main thread.

public voidrequestStatus()

Requests the latest status from the device.

Throws
IllegalStateException If there is no active service connection.
IllegalStateException If this method is not called on the main thread.
IOException

publicPendingResult<Status> sendMessage(Stringnamespace,Stringmessage)

Sends a message to the currently connected application.

Parameters
namespace The namespace for the message. Namespaces must begin with the prefix " urn:x-cast:".
message The message payload.
Returns
  • APendingResult which can be used to see whether the message has been enqueued to be sent to a Google Cast device, ornullif the session is not connected.
Throws
IllegalStateException If this method is not called on the main thread.

public voidsetMessageReceivedCallbacks(Stringnamespace, Cast.MessageReceivedCallback callbacks)

Sets the newCast.MessageReceivedCallback listener on this session for a given namespace. The new listener will replace an existing listener for a givennamespace.Messages received by the session for the givennamespacewill be forwarded to this listener.

Parameters
namespace The namespace of the Cast channel. Namespaces must begin with the prefix " urn:x-cast:".
callbacks TheCast.MessageReceivedCallback to perform callbacks on. May not benull.
Throws
IOException If an I/O error occurs while performing the request.
IllegalStateException If there is no active service connection.
IllegalArgumentException Ifnamespaceisnullor empty, or if the namespacedoesn't start with the prefix "urn:x-cast:".
IllegalStateException If this method is not called on the main thread.

public voidsetMute(boolean mute)

Mutes or unmutes the device's audio.

Throws
IOException If an I/O error occurs while performing the request.
IllegalStateException If there is no active service connection.
IllegalStateException If this method is not called on the main thread.

public voidsetVolume(double volume)

Sets the device volume. Ifvolumeis outside of the range [0.0, 1.0], then the value will be clipped.

Throws
IOException If an I/O error occurs while performing the request.
IllegalStateException If this method is not called on the main thread.

Protected Methods

protected voidend(boolean stopCasting)

Ends theCastSession. This method is called only by the SDK.

Parameters
stopCasting Should the receiver app be stopped when the current session ends.

protected voidonResuming(Bundle routeInfoExtra)

This method is called prior to resume(Bundle).Subclass should override this method to do necessary setup. The default implementation does nothing.

Parameters
routeInfoExtra the extra field of the selected media route and it can be obtained by MediaRouter.RouteInfo.getExtras()

protected voidonStarting(Bundle routeInfoExtra)

This method is called prior to start(Bundle).Subclass should override this method to do necessary setup. The default implementation does nothing.

Parameters
routeInfoExtra the extra field of the selected media route and it can be obtained by MediaRouter.RouteInfo.getExtras()

protected voidresume(Bundle routeInfoExtra)

Resumes theCastSession. This method is called only by the SDK.

Parameters
routeInfoExtra The extra field in theMediaRouter.RouteInfo.

protected voidstart(Bundle routeInfoExtra)

Starts theCastSession. This method is called only by the SDK.

Parameters
routeInfoExtra The extra field in theMediaRouter.RouteInfo.