Player basics
*************
The "Player"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/classes/Player/)
class is a player controller that is decoupled from the player view.
Create a Player instance using the "PlayerProvider"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/variables/PlayerProvider/),
and attach the video surface using the "PlayerView"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/functions/PlayerView/)
when needed. The player instance can be accessed with the "usePlayer"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/functions/usePlayer/)
hook to control playback. Other player hooks, such as "usePosition"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/functions/usePosition/)
and "useDuration"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/functions/useDuration/),
allow responding to player events and creating custom controls.
PlayerProvider
==============
The "PlayerProvider"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/variables/PlayerProvider/)
component initializes the player and makes it accessible through the
PlayerContext. It takes a player configuration to initialize a player
instance:
{/* component tree */}
"PlayerProvider"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/variables/PlayerProvider/)
can be placed anywhere in the component tree. It supports multiple
instances, with each "PlayerProvider"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/variables/PlayerProvider/)
managing its own player. Additionally, it is possible to nest multiple
"PlayerProvider"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/variables/PlayerProvider/)
components.
The number of concurrently active player instances is capped by
"SdkConfiguration.maxNumberOfPlayers"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/interfaces/SdkConfiguration/#maxnumberofplayers)
(default "2"). Creating an additional player past this limit raises an
"ErrorCode.MaxNumberOfPlayersExceeded"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/enumerations/ErrorCode/#maxnumberofplayersexceeded)
error. Raise the limit at SDK initialization if your application needs
more.
PlayerView
==========
The "PlayerView"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/functions/PlayerView/)
component creates a video surface and attaches it to the player. It
must be used within the scope of a "PlayerProvider"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/variables/PlayerProvider/).
However, the PlayerView doesn't need to be a direct child of the
"PlayerProvider"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/variables/PlayerProvider/).
It only needs to be within its scope. For example:
usePlayer
=========
The "usePlayer"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/functions/usePlayer/)
hook provides a programmatic way to interact with the player in
functional components. Similar to the "PlayerView"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/functions/PlayerView/)
component, the hook must be used within the scope of a
"PlayerProvider"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/variables/PlayerProvider/).
Sample usage:
const player = usePlayer();
player.pause();
Other player hooks can be used to build custom player controls. For
example, the "usePosition"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/functions/usePosition/)
and "useDuration"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/functions/useDuration/)
hooks can be used to build a time label:
export default function TimeLabel() {
const position = usePosition(); // in milliseconds
const duration = useDuration(); // in milliseconds
return (
<>
{position} / {duration}
>
);
}
For the full list of hooks, refer to the "API reference"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/).
Effective URL
=============
"Player.getEffectiveUrl()"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/classes/Player/#geteffectiveurl)
returns the URL the stream is actually loaded from, and the
"EventType.EffectiveUrlChanged" event announces every change of it. It
differs from the configured source URL when the Broadpeak plugin
redirects the stream to a Broadpeak session URL, which is the use case
this API supports. See the Broadpeak guide for details and examples.
Without the plugin the value is the configured source URL.
Picture-in-Picture
==================
Picture-in-Picture (PiP) is supported on Android, iOS, and web. The
player exposes it through "Player.supportsPictureInPicture"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/classes/Player/#supportspictureinpicture),
"Player.enterPictureInPictureMode"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/classes/Player/#enterpictureinpicturemode)
and "Player.exitPictureInPictureMode"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/classes/Player/#exitpictureinpicturemode),
and the current state can be observed with the
"usePictureInPictureMode"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/functions/usePictureInPictureMode/)
hook:
export default function PictureInPictureButton() {
const player = usePlayer();
const pictureInPictureMode = usePictureInPictureMode();
return (
);
}
Always check "Player.supportsPictureInPicture"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/classes/Player/#supportspictureinpicture)
before offering the control: it reflects the platform capability and,
on Android, whether the user has granted the PiP permission to the
application.
Android
-------
The activity hosting the player must declare PiP support in
"./android/app/src/main/AndroidManifest.xml":
By default the SDK adds its own controls to the PiP window — seek
backward 10 s, play/pause, and seek forward 10 s — and keeps them in
sync with the playback state.
If your application already provides its own PiP controls, set
"AndroidPlayerConfiguration.pictureInPictureActionsDisabled"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/interfaces/AndroidPlayerConfiguration/#pictureinpictureactionsdisabled)
to "true". The SDK then adds no controls of its own and never updates
the PiP parameters behind your back, leaving the window's actions
entirely under your control:
await player.open({
source: {
url: "https://demo.cf.castlabs.com/media/prestohls/master.m3u8",
type: ContentType.Hls,
},
android: {
pictureInPictureActionsDisabled: true,
},
});
Android PiP controls are "RemoteAction" objects, which require an
"Icon" and a "PendingIntent" and therefore cannot be created from
JavaScript — your own controls must be installed from native Android
code by calling "setPictureInPictureParams()" on your activity. Apply
them after the player has entered Picture-in-Picture, for example from
"Activity.onPictureInPictureModeChanged()", because entering PiP
clears the action list. Call "setPictureInPictureParams()" again
whenever your controls change, such as swapping a pause icon for a
play icon.
Apple
-----
On iOS and tvOS, two "ApplePlayerConfiguration"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/interfaces/ApplePlayerConfiguration/)
fields control the player's Picture-in-Picture and seeking behavior.
Set "apple.pictureInPictureAllowed"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/interfaces/ApplePlayerConfiguration/#pictureinpictureallowed)
to "false" to switch PiP off for a stream. With PiP disabled,
"Player.supportsPictureInPicture"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/classes/Player/#supportspictureinpicture)
reports "false", so a UI that checks it before offering the control
needs no further changes.
Set "apple.requiresLinearPlayback"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/interfaces/ApplePlayerConfiguration/#requireslinearplayback)
to "true" to stop the viewer skipping through the content — fast
forward, forward skip, and scrubbing are disabled. Use it to enforce
playback of mandatory content such as legalese or advertisements:
await player.open({
source: {
url: "https://demo.cf.castlabs.com/media/prestohls/master.m3u8",
type: ContentType.Hls,
},
apple: {
requiresLinearPlayback: true,
},
});
Which controls this covers depends on how playback is set up. With the
"AVPlayerViewControllerProvider"
(https://players.castlabs.com/react-native/1.4.2/docs/ios-avplayerviewcontroller-plugin-api/variables/AVPlayerViewControllerProvider/)
component the player supplies the whole playback UI, so its transport
bar loses skip and scrubbing for as long as the content is open. With
"PlayerProvider" and "" the inline controls are your own
and are left untouched — only the Picture-in-Picture window is
affected, so your controls must disable seeking and avoid calling
"Player.setPosition"
(https://players.castlabs.com/react-native/1.4.2/docs/sdk-api/classes/Player/#setposition)
themselves.
The field defaults to "false", leaving playback fully seekable.