Class Cast

java.lang.Object
com.codename1.media.Cast
Direct Known Subclasses:
GoogleCast

public class Cast extends Object

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();
}
  • Field Details

    • KIND_PLATFORM

      public static final int KIND_PLATFORM
      The platform route: AirPlay on iOS.
      See Also:
    • KIND_GOOGLE_CAST

      public static final int KIND_GOOGLE_CAST
      A Google Cast (Chromecast) session.
      See Also:
    • STATE_NO_SESSION

      public static final int STATE_NO_SESSION
      No session.
      See Also:
    • STATE_CONNECTING

      public static final int STATE_CONNECTING
      A session is starting.
      See Also:
    • STATE_CONNECTED

      public static final int STATE_CONNECTED
      A session is up and nothing is playing.
      See Also:
    • STATE_PLAYING

      public static final int STATE_PLAYING
      The receiver is playing.
      See Also:
    • STATE_PAUSED

      public static final int STATE_PAUSED
      The receiver is paused.
      See Also:
  • Method Details

    • setImplementation

      public static void setImplementation(com.codename1.impl.CodenameOneImplementation impl)

      Invoked internally from Display, this method is for internal use only

      Parameters
      • impl: 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: the media URL, reachable from the receiver

      • contentType: the MIME type, such as video/mp4

      • title: the title the receiver shows, or null

      • subtitle: the second line the receiver shows, or null

      • imageUrl: artwork, or null

      • startMillis: 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: 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: the listener
    • removeStateListener

      public void removeStateListener(SuccessCallback<Integer> listener)

      Removes a listener added with #addStateListener(SuccessCallback).

      Parameters
      • listener: the listener