Class StreamRequest
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 Summary
ConstructorsConstructorDescriptionStreamRequest(String url) Creates a request for the stream at the given URL. -
Method Summary
Modifier and TypeMethodDescriptionAdds an HTTP header to the manifest and segment requests.create()Creates the player.Creates the player without blocking the calling thread.getDrm()The content protection of the stream.The headers for the manifest and segment requests.The completion callback.intWhere playback starts.getUrl()The stream URL.booleanAnswers whether this platform can play the request: its DRM scheme, and its headers if it has any.Sets the content protection of the stream.setOnCompletion(Runnable onCompletion) Sets a callback invoked when playback reaches the end of the stream.setStartPosition(int millis) Sets where playback starts, for resuming a title the viewer left part way.
-
Constructor Details
-
StreamRequest
Creates a request for the stream at the given URL.
Parameters
url: the HLS (.m3u8) manifest, or a progressive file
-
-
Method Details
-
getUrl
The stream URL.
Returns
the manifest or file URL
-
setDrm
Sets the content protection of the stream.
Parameters
drm: the DRM configuration, or null for an unprotected stream
Returns
this request, for chaining
-
getDrm
The content protection of the stream.
Returns
the DRM configuration, or null for an unprotected stream
-
setStartPosition
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
Sets a callback invoked when playback reaches the end of the stream.
Parameters
onCompletion: the callback, or null
Returns
this request, for chaining
-
getOnCompletion
The completion callback.
Returns
the callback, or null
-
addHeader
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
-
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
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 formThrows
IOException: when the platform cannot play the request, whichisSupported()predicts, or the stream cannot be opened
- Throws:
IOException
-
createAsync
Creates the player without blocking the calling thread.
Returns
a resource completed with the media, or with the error
create()would throw
-