CastSession

public class CastSession extends Session

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

See Also

Public Method Summary

void
addCastListener(Cast.Listener listener)
Adds the Cast.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 null if the metadata is unavailable.
String
getApplicationStatus()
Returns the current receiver application status, if any.
CastDevice
RemoteMediaClient
getRemoteMediaClient()
Returns the RemoteMediaClient for remote media control.
long
getSessionRemainingTimeMs()
Returns the amount of time that this CastSession 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 the Cast.Listener.
void
removeMessageReceivedCallbacks(String namespace)
Removes the Cast.MessageReceivedCallback from this session for a given namespace.
void
PendingResult<Status>
sendMessage(String namespace, String message)
Sends a message to the currently connected application.
void
setMessageReceivedCallbacks(String namespace, Cast.MessageReceivedCallback callbacks)
Sets the new Cast.MessageReceivedCallback listener 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 the CastSession.
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 the CastSession.
void
start(Bundle routeInfoExtra)
Starts the CastSession.

Inherited Method Summary

Public Methods

public void addCastListener (Cast.Listener listener)

Adds the Cast.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 int getActiveInputState ()

Returns the device's active-input state. The returned value is ACTIVE_INPUT_STATE_YES, ACTIVE_INPUT_STATE_NO, or 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.ApplicationConnectionResult getApplicationConnectionResult ()

Returns the Cast.ApplicationConnectionResult, the result of launching or joining the receiver application when starting or resuming this session. Returns null if 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 null if the metadata is unavailable.

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

public String getApplicationStatus ()

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

public RemoteMediaClient getRemoteMediaClient ()

Returns the RemoteMediaClient for remote media control.

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

public long getSessionRemainingTimeMs ()

Returns the amount of time that this CastSession 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 int getStandbyState ()

Returns the device's standby state. The returned value is STANDBY_STATE_UNKNOWN, STANDBY_STATE_NO, or STANDBY_STATE_YES.

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

public double getVolume ()

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

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 void removeCastListener (Cast.Listener listener)

Removes the Cast.Listener.

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

public void removeMessageReceivedCallbacks (String namespace)

Removes the Cast.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 If namespace is null or empty.
IllegalStateException If this method is not called on the main thread.

public void requestStatus ()

public PendingResult<Status> sendMessage (String namespace, String message)

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
  • A PendingResult which can be used to see whether the message has been enqueued to be sent to a Google Cast device, or null if the session is not connected.
Throws
IllegalStateException If this method is not called on the main thread.

public void setMessageReceivedCallbacks (String namespace, Cast.MessageReceivedCallback callbacks)

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

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

public void setMute (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 void setVolume (double volume)

Sets the device volume. If volume is 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 void end (boolean stopCasting)

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

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

protected void onResuming (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 in the MediaRouter.RouteInfo.

protected void onStarting (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 in the MediaRouter.RouteInfo.

protected void resume (Bundle routeInfoExtra)

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

Parameters
routeInfoExtra The extra field in the MediaRouter.RouteInfo.

protected void start (Bundle routeInfoExtra)

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

Parameters
routeInfoExtra The extra field in the MediaRouter.RouteInfo.