Creates PlayerResource
objects, which are used to load and configure video sources independent of a component.
This method was created specifically when you want your application to render the same video player to multiple surfaces.
This means that for any video, you can create a player and pass it around to multiple screens in your application.
With the PlayerManager
module, you can avoid the overhead of re-initializing the stream and the creation of many stream requests!
Note that a video player can render to only one surface at a time.
PlayerManager
handles the lifecycle of your app’s player resources.
A PlayerResource
stores a unique internal tag that ties it to a native player resource.
You can configure the native player resource, via the methods on a PlayerResource
object.
To render the video, you must use either a VideoSurface
or a VideoSurfaceRef
component, but the video player resource is not dependent on, nor affected by, these components.
The following methods are supported with the PlayerManager module:
The following properties are supported with the PlayerResource object:
The following methods are supported with the PlayerResource object:
The following example shows PlayerManager
in use with a VideoSurfaceRef. You can alternatively use VideoSurfaceRef instead.
import React from "react";
import { PlayerManager, VideoSurface } from "@youi/react-native-youi";
import { View } from "react-native";
// Each player resource must be created with a unique tag
const VIDEO_TAG = "UNIQUE_VIDEO_TAG";
const App = () => {
const ref = React.useRef()
const [resource, setResource] = React.useState(null)
React.useEffect(()=>{
// Ensure the resource exists
let myPlayerResource = PlayerManager.create(VIDEO_TAG);
setResource(myPlayerResource)
return () => {
// Frees the player resource
// Automatically removes the `playing` listener that wasn't manually cleared
PlayerManager.destroy(myPlayerResource.tag);
}
},[])
React.useEffect(()=>{
if (!ref.current || !resource) return;
resource.loadSource({
uri: 'https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8',
type: 'HLS'
});
// Ensure your code creates the resource BEFORE setting
// the component's player to the resource's tag
ref.current.setPlayer(VIDEO_TAG)
let readyListener = resource.addEventListener('ready', () => {
resource.play();
readyListener.remove();
});
let playListener = resource.addEventListener('playing', () => {
playListener.remove();
});
},[ref.current, resource])
return (
<VideoSurface
style={{width:300, height:200}}
ref={ref}
/>
);
}
Creates a player resource native-side, and returns a JavaScript reference. Any configuration you need to do on the resource can be done on this returned reference. A string is supplied as a tag to the resource and uniquely identifies the native player resource.
create(resourceTag) -> PlayerResource
Name | Type | Required | Description |
---|---|---|---|
resourceTag |
string | Yes | Unique and non-empty string to identify the created player resource. |
Destroys the native-side resource associated with the provided resourceTag
, and the JavaScript-side reference is invalidated.
Use of a player resource reference after destroy
is called on it is a no-op.
destroy(resourceTag) -> void
Name | Type | Required | Description |
---|---|---|---|
resourceTag |
string | Yes | Unique and non-empty string to identify the target player resource. |
Performs a lookup in the JavaScript references held by the PlayerManager
that match the provided resource tag.
If the resource doesn’t exist, null
will be returned.
get(resourceTag) -> PlayerResource
Name | Type | Required | Description |
---|---|---|---|
resourceTag |
string | Yes | Unique and non-empty string to identify the target player resource. |
The internal tag of the resource you provided when you created it.
The returned tag is used as a link to the native-side resource.
When the PlayerManager
destroys the resource, this tag is be cleared, since no native-side resource is tied to the reference anymore.
Adds a listener for player events. Returns a subscription object to clean up the listener.
addEventListener(eventName, listener) -> object
Name | Type | Required | Description |
---|---|---|---|
eventName |
string | Yes | PlayerEventName |
listener |
function | Yes | Callback when the event is triggered. |
Name | Callback Parameter | Description |
---|---|---|
audioBitrateChange |
{tag: string, bitrateKbps: float} |
Invoked when the audio bitrate has changed. May be emitted at any point. The supplied bitrate is in kbps. |
availableAudioTracksChange |
{tag: string, tracks: [{id: number, name: string, language: string, valid: boolean}]} |
Signals that the available audio tracks for the current media have changed. May be emitted at any point. |
availableClosedCaptionsTracksChange |
{tag: string, tracks: [{id: number, name: string, language: string}]} |
Signals that the available closed caption tracks for the current media have changed. May be emitted at any point. |
bufferingEnd |
{tag: string} |
Invoked when player buffering has ended. An application can clear a buffering indicator, if one has been displayed, when this signal is emitted. The player is either in a playing or paused state depending on the state the player was in prior to buffering. Currently not supported for the Roku target platform. |
bufferingStart |
{tag: string} |
Invoked when player buffering has started. An application can display a buffering indicator when this signal is emitted. Currently not supported for the Roku target platform. |
currentTimeUpdate |
{tag: string, currentTime: number} |
Provides the current time of the video, in milliseconds. Emitted every 100ms by default, but this threshold can be configured using setCurrentTimeUpdateThreshold . |
durationChange |
{tag: string, duration: number} |
Invoked when the duration of the loaded video media has changed. Typically occurs after loading new video media. The supplied duration is in milliseconds. |
error |
{tag: string, error: {errorCode: string, nativePlayerErrorCode: string, message: string}} |
Invoked when the player reports an error. When using ExoPlayer with Android, the value for nativePlayerErrorCode will always be empty. |
finalize |
{tag: string} |
Invoked once the player has shut down and been cleaned up. This signal is sent on stop() . However, in order for finalize to be triggered, the video player must be finished preparing. |
pause |
{tag: string} |
Invoked when playback has paused. |
playbackComplete |
{tag: string} |
Invoked when the playback of the currently loaded video media has completed. After this signal is emitted the player is in the paused state. |
playing |
{tag: string} |
Invoked when playback has begun - either the initial playback or a resume after the paused or buffering states. |
preparing |
{tag: string} |
Invoked when the player has started preparing a video for playback. Occurs only when the target platform supports the provided video format. Currently not supported for the Roku target platform. |
ready |
{tag: string} |
Invoked when the native player is ready to be interacted with and information about the player can be queried. Currently not supported for the Roku target platform. |
totalBitrateChange |
{tag: string, bitrateKbps: float} |
Invoked when the combined video and audio bitrate has changed. May be emitted at any point. The supplied bitrate is in kbps. |
videoBitrateChange |
{tag: string, bitrateKbps: float} |
Invoked when the video bitrate has changed. May be emitted at any point. The supplied bitrate is in kbps. |
Returns a promise with an object containing player statistics.
getStatistics() -> Promise<Object>
Data | Type |
---|---|
audioBitrateKbps |
number |
bufferLengthMs |
number |
defaultAudioBitrateKbps |
number |
defaultTotalBitrateKbps |
number |
defaultVideoBitrateKbps |
number |
framesPerSecond |
number |
isLive |
boolean |
minimumBufferLengthMs |
number |
totalBitrateKbps |
number |
videoBitrateKbps |
number |
Load either a remote URL or a local file resource.
loadSource(src) -> void
Name | Type | Required | Description |
---|---|---|---|
src |
{type: string, uri: Object, headers: Object, drmScheme: string, drmInfo: Object} |
Yes | Video source to load. |
drmScheme | drmInfo Type | Notes |
---|---|---|
fairplay |
null |
See FairPlayDRMHandler Module |
istreamplanet_fairplay |
null |
|
none |
null |
This is the default. |
playready |
string |
|
widevine_modular |
{licenseAcquisitionUrl: string, headers: Array<{ key: string, value: string }> } |
|
widevine_modular_custom_request |
{licenseAcquisitionUrl: string, headers: Array<{ key: string, value: string }> } |
See WidevineCustomRequestDRMHandler Module |
Pauses video playback.
pause() -> void
Starts playback of the native player, or resumes if the player is paused. The video must be in the ready state first.
play() -> void
Removes all listeners for the player resource or, optionally, all the listeners for a given event on a player resource.
removeAllListeners(eventName?) -> void
Name | Type | Required | Description |
---|---|---|---|
eventName |
string | No | PlayerEventName. If provided, all listeners registered for this event on the resource will be removed. |
Removes a listener for a player event.
removeEventListener(eventName, listener) -> void
Name | Type | Required | Description |
---|---|---|---|
eventName |
string | Yes | PlayerEventName |
listener |
function | Yes | Callback to remove. Must be registered for the event name provided. |
Seeks the video to a specific position in the video media.
Returns a promise object. If the seek operation is successful, the returned promise will include the new stream position, in milliseconds. If the seek operation fails, the promise rejection will include the current stream position, in milliseconds.
seek(positionInMs) -> Promise<number>
Name | Type | Required | Description |
---|---|---|---|
seek |
number | Yes | The desired stream position, in milliseconds. |
Customize the video playback buffer length.
setBufferLength(bufferLength) -> void
Name | Type | Required | Description |
---|---|---|---|
bufferLength |
{min: number, max: number} |
Yes |
min : The minimum buffer length, in milliseconds. The video player will not let the buffer length fall below this value. max : The maximum buffer length, in milliseconds. The video player will not let the buffer length exceed this value. |
Sets the maximum bitrate to the number specified, in bits per second. The player then uses this value when streaming from an adaptive media source. By default, no bitrate restriction is applied.
maxBitrate
is set during playback, it won’t take effect until content that’s already been buffered has played back.maxBitrate
to 0
removes any bitrate restrictions.maxBitrate
must be set before preparing the media.MaxBandwidth
metadata attribute before preparing media and starting playback.setMaxBitrate(bitsPerSecond) -> void
Name | Type | Required | Description |
---|---|---|---|
bitsPerSecond |
number | Yes | The maximum bitrate, in bits per second. |
Sets how often the player event currentTimeUpdate
is fired.
The default threshold is 100ms.
Note: This method sets the minimum threshold. The actual time between updates may be greater depending on the underlying player.
setCurrentTimeUpdateThreshold(currentTimeUpdateThreshold) -> void
Name | Type | Required | Description |
---|---|---|---|
currentTimeUpdateThreshold |
number | Yes | How often the event currentTimeUpdate should be fired, in milliseconds. |
Configures the muted property of the video.
Defaults to false
.
setMuted(isMuted) -> void
Name | Type | Required | Description |
---|---|---|---|
isMuted |
boolean | No | True if the video should be muted. |
Switches the video player’s audio to the track indicated by the given ID. If the video is playing, playback will not be stopped during the switch.
setSelectedAudioTrack(id) -> void
Name | Type | Required | Description |
---|---|---|---|
id |
number | Yes | The ID of the desired audio track. |
Switches the video player’s Closed Captions to the track indicated by the given ID. If the video is playing, playback will not be stopped during the switch.
setSelectedClosedCaptionsTrack(id) -> void
Name | Type | Required | Description |
---|---|---|---|
id |
number | No | The ID of the desired Closed Captions track. Omitting the ID or passing null disables closed captioning. |
Configures the start time of the video stream, in milliseconds. By default, VOD starts at 0 and a live stream starts from the live head. This should be set before loadSource is called.
setStartTime(millis) -> void
Name | Type | Required | Description |
---|---|---|---|
millis |
number | Yes | Start time of the video, in milliseconds. |
Sets the User Agent to be used by the player for requests on manifests and video segments.
""
).
The default User Agent varies per platform.setUserAgent(userAgent) -> void
Name | Type | Required | Description |
---|---|---|---|
userAgent |
string | Yes | The User Agent. |
Stops video playback. Allows users to switch between multiple videos and to unload media.
stop() -> void