Listening to events
There are two ways to receive Player and Source events:
Player.onandSource.on(since v1.31.0): subscribe to events in code, independent of anyPlayerView. Use this to react to events of a player that is not shown (yet), or to receive the events of a specificSource.PlayerViewevent props: pass callbacks such asonReadyto thePlayerView. The view forwards them toPlayer.onof its player. Events that are specific to thePlayerView, such asonFullscreenEnter, are only available as props.
For a complete list of the available events, check PlayerEventMap and SourceEventMap and events.ts.
Player events via Player.on
Player.onPlayer.on registers a listener for an event name, and Player.off removes it again:
import { useEffect } from 'react';
import {
PlayerErrorEvent,
ReadyEvent,
usePlayer,
} from 'bitmovin-player-react-native';
export default function PlayerScreen() {
const player = usePlayer();
useEffect(() => {
const onReady = (event: ReadyEvent) => {
console.log('Ready', event.timestamp);
};
const onPlayerError = (event: PlayerErrorEvent) => {
console.error('Player error', event.message);
};
player.on('ready', onReady);
player.on('playerError', onPlayerError);
return () => {
player.off('ready', onReady);
player.off('playerError', onPlayerError);
};
}, [player]);
// ...
}- The player emits its own events, and the source events of the active source, e.g.
sourceLoaded. - Listeners can be registered before the player is initialized or before a source is loaded. They also receive the events of that load, such as
playerActive. - All listeners are removed when the player is destroyed.
- Registering the same listener twice for an event has no effect.
Source events via Source.on
Source.onSource.on receives all events of one specific source, including while it is not the active source of a player. This is useful with playlists, e.g. to know when a preloaded source has been loaded:
import {
Source,
SourceLoadedEvent,
SourceType,
} from 'bitmovin-player-react-native';
const source = new Source({
url: 'https://cdn.bitmovin.com/content/assets/streams-sample-video/sintel/m3u8/index.m3u8',
type: SourceType.HLS,
title: 'Sintel',
});
const onSourceLoaded = (event: SourceLoadedEvent) => {
console.log(`${source.config?.title} is loaded`, event.source.duration);
};
source.on('sourceLoaded', onSourceLoaded);
player.loadSource(source);
// Later, once the listener is no longer needed:
source.off('sourceLoaded', onSourceLoaded);- Listeners can be registered before the source is loaded.
- All listeners are removed when the source is destroyed.
Player.on or Source.on for Sources?
Player.ononly receives the source events of the source that is currently active. The only exception issourceError, which the player also emits for inactive sources, without identifying the source. UseSource.onfor the events of a specific source.
PlayerView event props
PlayerView event propsTo register an event callback on the PlayerView, use the event name prefixed with on as a property:
const onReady = useCallback((event: ReadyEvent) => {
console.log('Ready event');
}, []);
...
return (
<PlayerView
player={player}
onReady={onReady}
onMuted={onMuted}
onPaused={onPaused}
onPlayerActive={onPlayerActive}
onSourceLoaded={onSourceLoaded}
onPlayerError={onPlayerError}
onSourceError={onSourceError}
onPlaybackFinished={onPlaybackFinished}
...
/>
);The PlayerView registers these props via Player.on of its player while it is mounted, and removes them via Player.off when it unmounts. They therefore receive the same events as Player.on, but only while the view is mounted. Fullscreen and picture-in-picture events, such as onFullscreenEnter, come from the view itself and are only available as props.
Known limitations
- On Android,
SourceUnloadedEvent.sourceis not set for events received viaPlayer.onor thePlayerViewprops, as the native event does not carry the source. UseSource.onto receive it with the source. Source.onlisteners do not receive the finalsourceUnloadedof a source that is destroyed together with its player.Player.onlisteners and theonSourceUnloadedprop receive it for the active source.
Updated 8 days ago