Summary: Methods | Protected Methods | Inherited Methods | [Expand All]
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

- `Session`

## 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.
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
abstract void | end(boolean stopCasting) Subclass should override this method to provide business logic of ending a session.
final String | getCategory() Returns the category associated with this `Session`.
final String | getSessionId() Returns the unique ID for this session.
long | getSessionRemainingTimeMs() Subclass can override this method to provide a hint of how long the current session will live.
boolean | isConnected() Returns `true` if the session is connected.
boolean | isConnecting() Returns `true` if the session is connecting or resuming.
boolean | isDisconnected() Returns `true` if the session is disconnected.
boolean | isDisconnecting() Returns `true` if the session is disconnecting.
boolean | isResuming() Returns `true` if the session is resuming.
boolean | isSuspended() Returns `true` if the session is suspended.
final void | notifyFailedToResumeSession(int error) Subclass should call this to notify the SDK that a session has failed to resume.
final void | notifyFailedToStartSession(int error) Subclass should call this to notify the SDK that a session has failed to start.
final void | notifySessionEnded(int error) Subclass should call this to notify the SDK that a session has ended.
final void | notifySessionResumed(boolean wasSuspended) Subclass should call this to notify the SDK that a session has been successfully resumed.
final void | notifySessionStarted(String sessionId) Subclass should call this to notify the SDK that a session has been successfully started.
final void | notifySessionSuspended(int reason) Subclass should call this to notify the SDK that a session has been suspended.
abstract void | resume(Bundle routeInfoExtra) Subclass should override this method to provide business logic of resuming a session.
abstract void | start(Bundle routeInfoExtra) Subclass should override this method to provide business logic of starting a session.
From class java.lang.Object
Object | clone()
boolean | equals(Object arg0)
void | finalize()
final Class<?> | getClass()
int | hashCode()
final void | notify()
final void | notifyAll()
String | toString()
final void | wait(long arg0, int arg1)
final void | wait(long arg0)
final void | wait()

## 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.

#### 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.

#### 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.

#### 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.

#### public CastDevice getCastDevice ()

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

#### public RemoteMediaClient getRemoteMediaClient ()

Returns the `RemoteMediaClient` for remote media control.

#### 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.

#### 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.

#### public double getVolume ()

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

##### Throws

IllegalStateException | If there is no active service connection.

#### public boolean isMute ()

Returns the device's mute state.

##### Throws

IllegalStateException | If there is no active service connection.

#### public void removeCastListener (Cast.Listener listener)

Removes the `Cast.Listener`.

#### 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.

#### 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.

#### 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.

#### 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:`".

#### 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.

#### 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.

## 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 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.