Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit fef127c

Browse files
committed
Update readme with examples of using hlsConfig and playerRef
1 parent 3692321 commit fef127c

2 files changed

Lines changed: 99 additions & 2 deletions

File tree

README.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ It uses [hls.js](https://github.com/video-dev/hls.js) to play your hls live stre
1212

1313
## Examples
1414

15+
### Using the ReactHlsPlayer component
16+
1517
```javascript
1618
import React from 'react';
1719
import ReactDOM from 'react-dom';
1820
import ReactHlsPlayer from 'react-hls-player';
1921

20-
// General Usage
2122
ReactDOM.render(
2223
<ReactHlsPlayer
2324
src="https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8"
@@ -30,6 +31,102 @@ ReactDOM.render(
3031
);
3132
```
3233

34+
### Using hlsConfig (advanced use case)
35+
36+
All available config properties can be found on the [Fine Tuning](https://github.com/video-dev/hls.js/blob/master/docs/API.md#fine-tuning) section of the Hls.js API.md
37+
38+
```javascript
39+
import React from 'react';
40+
import ReactDOM from 'react-dom';
41+
import ReactHlsPlayer from 'react-hls-player';
42+
43+
ReactDOM.render(
44+
<ReactHlsPlayer
45+
src="https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8"
46+
hlsConfig={{
47+
maxLoadingDelay: 4,
48+
minAutoBitrate: 0,
49+
lowLatencyMode: true,
50+
}}
51+
/>,
52+
document.getElementById('app')
53+
);
54+
```
55+
56+
### Using playerRef
57+
58+
The `playerRef` returns a ref to the underlying video component, and as such will give you access to all video component properties and methods.
59+
60+
```javascript
61+
import React from 'react';
62+
import ReactHlsPlayer from 'react-hls-player';
63+
64+
function MyCustomComponent() {
65+
const playerRef = React.useRef();
66+
67+
function playVideo() {
68+
playerRef.current.play();
69+
}
70+
71+
function pauseVideo() {
72+
playerRef.current.pause();
73+
}
74+
75+
function toggleControls() {
76+
playerRef.current.controls = !playerRef.current.controls;
77+
}
78+
79+
return (
80+
<ReactHlsPlayer
81+
playerRef={playerRef}
82+
src="https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8"
83+
/>
84+
);
85+
}
86+
87+
ReactDOM.render(<MyCustomComponent />, document.getElementById('app'));
88+
```
89+
90+
You can also listen to events of the video
91+
92+
```javascript
93+
import React from 'react';
94+
import ReactHlsPlayer from 'react-hls-player';
95+
96+
function MyCustomComponent() {
97+
const playerRef = React.useRef();
98+
99+
React.useEffect(() => {
100+
function fireOnVideoStart() {
101+
// Do some stuff when the video starts/resumes playing
102+
}
103+
104+
playerRef.current.addEventListener('play', fireOnVideoStart);
105+
106+
return playerRef.current.removeEventListener('play', fireOnVideoStart);
107+
}, []);
108+
109+
React.useEffect(() => {
110+
function fireOnVideoEnd() {
111+
// Do some stuff when the video ends
112+
}
113+
114+
playerRef.current.addEventListener('ended', fireOnVideoEnd);
115+
116+
return playerRef.current.removeEventListener('ended', fireOnVideoEnd);
117+
}, []);
118+
119+
return (
120+
<ReactHlsPlayer
121+
playerRef={playerRef}
122+
src="https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8"
123+
/>
124+
);
125+
}
126+
127+
ReactDOM.render(<MyCustomComponent />, document.getElementById('app'));
128+
```
129+
33130
## Props
34131

35132
All [video properties](https://www.w3schools.com/tags/att_video_poster.asp) are supported and passed down to the underlying video component

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-hls-player",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"description": "A simple and easy to use react component for playing an hls live stream",
55
"main": "./dist/index.js",
66
"types": "./dist",

0 commit comments

Comments
 (0)