Class StreamRequest

java.lang.Object
com.codename1.media.StreamRequest

public final class StreamRequest extends Object

Describes an adaptive (HLS) or progressive video stream, optionally protected by DRM, and creates the platform player for it.

String manifestUrl = "https://example.com/video/master.m3u8";
String licenseUrl = "https://example.com/license/widevine";
String token = "token-from-your-server";
StreamRequest req = new StreamRequest(manifestUrl)
        .setDrm(new DrmConfig(DrmScheme.WIDEVINE, licenseUrl)
                .addLicenseHeader("Authorization", "Bearer " + token))
        .setStartPosition(30000);
if (!req.isSupported()) {
    // ask the server for a scheme this device can play
}
Form form = new Form("Player", new BorderLayout());
req.createAsync().ready(video -> {
    form.add(BorderLayout.CENTER, video.getVideoComponent());
    form.revalidate();
});
form.show();

The player is the platform's own (AVPlayer on Apple platforms, the framework media player on Android, ffmpeg in the simulator), so the protocols, codecs and DRM schemes it plays are the platform's. isSupported() answers for the scheme and the headers; a codec the device cannot decode surfaces as a playback error.

To hide a protected video from screenshots and screen recording while it is on screen, call Display.getInstance().setProperty("DisableScreenshots", "true") and set it back to "false" when the player closes.

  • Constructor Details

    • StreamRequest

      public StreamRequest(String url)

      Creates a request for the stream at the given URL.

      Parameters
      • url: the HLS (.m3u8) manifest, or a progressive file
  • Method Details

    • getUrl

      public String getUrl()

      The stream URL.

      Returns

      the manifest or file URL

    • setDrm

      public StreamRequest setDrm(DrmConfig drm)

      Sets the content protection of the stream.

      Parameters
      • drm: the DRM configuration, or null for an unprotected stream
      Returns

      this request, for chaining

    • getDrm

      public DrmConfig getDrm()

      The content protection of the stream.

      Returns

      the DRM configuration, or null for an unprotected stream

    • setStartPosition

      public StreamRequest setStartPosition(int millis)

      Sets where playback starts, for resuming a title the viewer left part way.

      Parameters
      • millis: the start position in milliseconds
      Returns

      this request, for chaining

    • getStartPosition

      public int getStartPosition()

      Where playback starts.

      Returns

      the start position in milliseconds

    • setOnCompletion

      public StreamRequest setOnCompletion(Runnable onCompletion)

      Sets a callback invoked when playback reaches the end of the stream.

      Parameters
      • onCompletion: the callback, or null
      Returns

      this request, for chaining

    • getOnCompletion

      public Runnable getOnCompletion()

      The completion callback.

      Returns

      the callback, or null

    • addHeader

      public StreamRequest addHeader(String name, String value)

      Adds an HTTP header to the manifest and segment requests. Only some platforms can attach headers to the requests their player makes (see MediaManager.isStreamHeadersSupported()); a URL that carries its own signed token works everywhere and is the portable choice.

      Parameters
      • name: the header name

      • value: the header value

      Returns

      this request, for chaining

    • getHeaders

      public Map<String,String> getHeaders()

      The headers for the manifest and segment requests.

      Returns

      a copy of the headers, keyed by name

    • isSupported

      public boolean isSupported()

      Answers whether this platform can play the request: its DRM scheme, and its headers if it has any.

      Returns

      true when create() can be expected to return a player

    • create

      public Media create() throws IOException

      Creates the player. Blocks until the platform has created it, so call it off the EDT or use createAsync().

      Returns

      the media, whose getVideoComponent() is the view to place in a form

      Throws
      • IOException: when the platform cannot play the request, which isSupported() predicts, or the stream cannot be opened
      Throws:
      IOException
    • createAsync

      public AsyncResource<Media> createAsync()

      Creates the player without blocking the calling thread.

      Returns

      a resource completed with the media, or with the error create() would throw