public class

CastSession

extends Session
java.lang.Object
   ↳ com.google.android.gms.cast.framework.Session
     ↳ com.google.android.gms.cast.framework.CastSession

Class Overview

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:

Summary

Public Methods
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 getCastDevice()
Returns the connecting or connected CastDevice, or null if the session is disconnecting or disconnected.
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 requestStatus()
Requests the receiver's current status.
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 Methods
void end(boolean stopCasting)
Ends the CastSession.
void resume(Bundle routeInfoExtra)
Resumes the CastSession.
void start(Bundle routeInfoExtra)
Starts the CastSession.
[Expand]
Inherited Methods
From class com.google.android.gms.cast.framework.Session
From class java.lang.Object

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.

Parameters
listener Cast.Listener
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.

Returns
int
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.

Returns
Cast.ApplicationConnectionResult
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.

Returns
ApplicationMetadata
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.

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

public CastDevice getCastDevice ()

Returns the connecting or connected CastDevice, or null if the session is disconnecting or disconnected.

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

public RemoteMediaClient getRemoteMediaClient ()

Returns the RemoteMediaClient for remote media control.

Returns
RemoteMediaClient
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.

Returns
long
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.

Returns
int
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].

Returns
double
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.

Returns
boolean
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.

Parameters
listener 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 String: 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 ()

Requests the receiver's current status.

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 PendingResult<Status> sendMessage (String namespace, String message)

Sends a message to the currently connected application.

Parameters
namespace String: The namespace for the message. Namespaces must begin with the prefix " urn:x-cast:".
message String: The message payload.
Returns
PendingResult<Status> 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 String: The namespace of the Cast channel. Namespaces must begin with the prefix "urn:x-cast:".
callbacks Cast.MessageReceivedCallback: 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.

Parameters
mute boolean
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.

Parameters
volume double
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 boolean: Should the receiver app be stopped when the current session ends.

protected void resume (Bundle routeInfoExtra)

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

Parameters
routeInfoExtra Bundle: 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 Bundle: The extra field in the MediaRouter.RouteInfo.