Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public class Cast

  1. Object
  2. Cast

Known subtypesGoogleCast

Sends media to a TV or speaker: discovers routes, shows the route picker and controls the remote session.

#getPlatform() is the route the platform has without extra libraries: AirPlay on iOS, through the system route picker. With AirPlay the device keeps playing the media itself and streams it to the route, so #load(String, String, String, String, String, long) answers false there and the app plays through MediaManager as usual. GoogleCast is the Chromecast session, where the receiver fetches and plays the URL itself.

Unlike the static device services (PictureInPicture, Battery), this is an instance API: a device can have more than one kind of route at once – AirPlay and Google Cast on the same iPhone – each with its own session and state, so each kind is an object of its own with its own listeners.

Cast cast = Cast.getPlatform();
if (cast.isSupported()) {
    cast.showRoutePicker();
}

Fields

public static final int KIND_PLATFORM = 0The platform route: AirPlay on iOS.
public static final int KIND_GOOGLE_CAST = 1A Google Cast (Chromecast) session.
public static final int STATE_NO_SESSION = 0No session.
public static final int STATE_CONNECTING = 1A session is starting.
public static final int STATE_CONNECTED = 2A session is up and nothing is playing.
public static final int STATE_PLAYING = 3The receiver is playing.
public static final int STATE_PAUSED = 4The receiver is paused.

Methods

public static void setImplementation(CodenameOneImplementation impl)Invoked internally from Display, this method is for internal use only
public static Cast getPlatform()The platform route, AirPlay on iOS.
public int getKind()The kind of route this instance controls.
public boolean isSupported()Answers whether this kind of route works on this device.
public boolean showRoutePicker()Shows the system route picker, where the user discovers and picks a device.
public boolean isSessionActive()Answers whether a session to a device is up.
public boolean load(String url, String contentType, String title, String subtitle, String imageUrl, long startMillis)Asks the receiver to load and play a URL.
public boolean play()Resumes playback on the receiver.
public boolean pause()Pauses playback on the receiver.
public boolean seek(long millis)Seeks on the receiver.
public boolean stop()Stops playback and ends the session.
public int getState()The last session state a port reported.
public void addStateListener(SuccessCallback<Integer> listener)Adds a listener for session state changes; it receives a STATE_ constant on the EDT.
public void removeStateListener(SuccessCallback<Integer> listener)Removes a listener added with #addStateListener(SuccessCallback).

Inherited methods

Field details

KIND_PLATFORM

public static final int KIND_PLATFORM = 0
The platform route: AirPlay on iOS.

KIND_GOOGLE_CAST

public static final int KIND_GOOGLE_CAST = 1
A Google Cast (Chromecast) session.

STATE_NO_SESSION

public static final int STATE_NO_SESSION = 0
No session.

STATE_CONNECTING

public static final int STATE_CONNECTING = 1
A session is starting.

STATE_CONNECTED

public static final int STATE_CONNECTED = 2
A session is up and nothing is playing.

STATE_PLAYING

public static final int STATE_PLAYING = 3
The receiver is playing.

STATE_PAUSED

public static final int STATE_PAUSED = 4
The receiver is paused.

Method details

setImplementation

public static void setImplementation(CodenameOneImplementation impl)
Invoked internally from Display, this method is for internal use only

Parameters

impl CodenameOneImplementation
implementation instance

getPlatform

public static Cast getPlatform()
The platform route, AirPlay on iOS.

Returns

the shared instance

getKind

public int getKind()
The kind of route this instance controls.

Returns

KIND_PLATFORM or KIND_GOOGLE_CAST

isSupported

public boolean isSupported()
Answers whether this kind of route works on this device.

Returns

true when the route picker can be shown

showRoutePicker

public boolean showRoutePicker()
Shows the system route picker, where the user discovers and picks a device. On iOS this is best effort: there is no public call that opens the AirPlay picker, so the port taps the button inside the system’s route picker view, and answers false when it finds none.

Returns

false when the picker cannot be shown here

isSessionActive

public boolean isSessionActive()
Answers whether a session to a device is up.

Returns

true while connected

load

public boolean load(String url, String contentType, String title, String subtitle, String imageUrl, long startMillis)
Asks the receiver to load and play a URL.

Parameters

url String
the media URL, reachable from the receiver
contentType String
the MIME type, such as video/mp4
title String
the title the receiver shows, or null
subtitle String
the second line the receiver shows, or null
imageUrl String
artwork, or null
startMillis long
the position to start at

Returns

false when there is no session or the route plays locally (AirPlay)

play

public boolean play()
Resumes playback on the receiver.

Returns

false when there is no remote session

pause

public boolean pause()
Pauses playback on the receiver.

Returns

false when there is no remote session

seek

public boolean seek(long millis)
Seeks on the receiver.

Parameters

millis long
the position

Returns

false when there is no remote session

stop

public boolean stop()
Stops playback and ends the session.

Returns

false when there is no remote session

getState

public int getState()
The last session state a port reported.

Returns

one of the STATE_ constants

addStateListener

public void addStateListener(SuccessCallback<Integer> listener)
Adds a listener for session state changes; it receives a STATE_ constant on the EDT.

Parameters

listener SuccessCallback<Integer>
the listener

removeStateListener

public void removeStateListener(SuccessCallback<Integer> listener)
Removes a listener added with #addStateListener(SuccessCallback).

Parameters

listener SuccessCallback<Integer>
the listener