Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: reinitialize player on changes in player config or UI config #12

Merged
merged 8 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Example ([#10](https://github.com/bitmovin/bitmovin-player-react/pull/10))
- Contribution guide ([#10](https://github.com/bitmovin/bitmovin-player-react/pull/10))
- Issue template ([#10](https://github.com/bitmovin/bitmovin-player-react/pull/10))
- Reinitialize the player on changes in the player config or UI config ([#12](https://github.com/bitmovin/bitmovin-player-react/pull/12))

### Changed

Expand Down
76 changes: 66 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function MyComponent() {

## Dynamically update player source config

`BitmovinPlayer` keeps track of the source config and reloads the player when the source config changes:
`BitmovinPlayer` keeps track of the source config and reloads the source on changes:

```tsx
const playerSources: Array<SourceConfig | undefined> = [
Expand Down Expand Up @@ -87,6 +87,56 @@ export function MyComponent() {
}
```

## Dynamically update player config and UI config

`BitmovinPlayer` keeps track of the player config and UI config and reinitializes the player (destroys the old instance and creates a new one) on changes :

```ts
const playerConfigs: Array<PlayerConfig> = [
{
key: "<key>",
playback: {
autoplay: true,
muted: true,
},
},
{
key: "<key>",
playback: {
autoplay: false,
muted: false,
},
},
];

export function MyComponent() {
const [playerConfig, setPlayerConfig] = useState(playerConfigs[0]);

useEffect(() => {
let lastConfigIndex = 0;

const intervalId = setInterval(() => {
const newIndex = ++lastConfigIndex % playerConfigs.length;

console.log(`Switching to player config ${newIndex}`, playerConfigs[newIndex]);

setPlayerConfig(playerConfigs[newIndex]);
}, 15_000);

return () => clearInterval(intervalId);
}, []);

return (
<Fragment>
<h1>Dynamic player config demo</h1>
<BitmovinPlayer config={playerConfig} source={playerSource} />
</Fragment>
);
}
```

The same applies to the `customUi` object.

## Attach event listeners

```tsx
Expand Down Expand Up @@ -145,24 +195,26 @@ export function MyComponent() {
You can use `UIContainer` from https://www.npmjs.com/package/bitmovin-player-ui to customize the player UI:

```tsx
import { PlaybackToggleOverlay, UIContainer } from "bitmovin-player-ui";
import { PlaybackToggleOverlay, UIContainer, CustomUi } from "bitmovin-player-ui";

// Ensure this function returns a new instance of the `UIContainer` on every call.
const uiContainerFactory = () =>
new UIContainer({
components: [new PlaybackToggleOverlay()],
});

const customUi: CustomUi = {
containerFactory: uiContainerFactory
};

export function MyComponent() {
return (
<Fragment>
<h1>UI container demo</h1>
<BitmovinPlayer
source={playerSource}
config={playerConfig}
customUi={{
containerFactory: uiContainerFactory,
}}
customUi={customUi}
/>
</Fragment>
);
Expand All @@ -174,7 +226,7 @@ export function MyComponent() {
You can use `UIVariant`s from https://www.npmjs.com/package/bitmovin-player-ui to customize the player UI:

```tsx
import { UIVariant } from "bitmovin-player-ui";
import { UIVariant, CustomUi } from "bitmovin-player-ui";

// Ensure this function returns a new instance of the `UIVariant[]` on every call.
const uiVariantsFactory = (): UIVariant[] => [
Expand All @@ -198,16 +250,18 @@ const uiVariantsFactory = (): UIVariant[] => [
},
];

const customUi: CustomUi = {
variantsFactory: uiVariantsFactory
};

export function MyComponent() {
return (
<Fragment>
<h1>UI variants demo</h1>
<BitmovinPlayer
source={playerSource}
config={playerConfig}
customUi={{
variantsFactory: uiVariantsFactory,
}}
customUi={customUi}
/>
</Fragment>
);
Expand Down Expand Up @@ -274,7 +328,7 @@ export function MyComponent() {

## Possible pitfalls

### Avoid source object recreation on every render
### Avoid player config, UI config, and source objects recreation on every render

```tsx
export function MyComponent() {
Expand Down Expand Up @@ -308,6 +362,8 @@ Instead do one of the following:
- Use `useState` (refer to the "Dynamic source demo" above)
- Use `useMemo`:

The same applies to the `config`, `source`, and `customUi` objects.

```tsx
export function MyComponent() {
const [_counter, setCounter] = useState(0);
Expand Down
39 changes: 38 additions & 1 deletion example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react": "^4.2.1",
"vite": "^5.2.7"
}
Expand Down
14 changes: 6 additions & 8 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PlayerConfig, SourceConfig } from 'bitmovin-player';
import { BitmovinPlayer } from 'bitmovin-player-react';
import { BitmovinPlayer, CustomUi } from 'bitmovin-player-react';
import { ControlBar, PlaybackToggleOverlay, SeekBar, UIContainer, UIVariant } from 'bitmovin-player-ui';
import { Fragment } from 'react';

Expand Down Expand Up @@ -39,6 +39,10 @@ const uiVariantsFactory = (): UIVariant[] => [
},
];

const customUi: CustomUi = {
variantsFactory: uiVariantsFactory,
};

export function App() {
return (
<Fragment>
Expand All @@ -49,13 +53,7 @@ export function App() {
maxWidth: '800px',
}}
>
<BitmovinPlayer
source={defaultPlayerSource}
config={playerConfig}
customUi={{
variantsFactory: uiVariantsFactory,
}}
/>
<BitmovinPlayer source={defaultPlayerSource} config={playerConfig} customUi={customUi} />
</div>
</Fragment>
);
Expand Down
Loading