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

Add HLS playback support for web browsers #4352

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@typescript-eslint/eslint-plugin": "^6.7.4",
"eslint": "^8.19.0",
"eslint-plugin-jest": "^27.4.2",
"hls.js": "^1.5.18",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add this also as optional peer dependency

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KrzysztofMoch like this?

"peerDependencies": {
        "react": "*",
        "react-native": "*",
        "hls.js": "^1.5.18"
 },

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bhyoo99 The best would be to put it in optionnalDependency: https://docs.npmjs.com/cli/v8/configuring-npm/package-json#optionaldependencies
It will allow to not embed the package is not needed.
It would be interesting to put the information in the documentation also.

"jest": "^29.7.0",
"prettier": "^2.4.1",
"react": "18.2.0",
Expand Down
24 changes: 24 additions & 0 deletions src/Video.web.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Hls from 'hls.js';
import React, {
forwardRef,
useCallback,
Expand Down Expand Up @@ -252,6 +253,29 @@ const Video = forwardRef<VideoRef, ReactVideoProps>(
nativeRef.current.playbackRate = rate;
}, [rate]);

useEffect(() => {
if (!nativeRef.current || typeof src?.uri !== 'string') {
return;
}

const isHlsUrl = src.uri.endsWith('.m3u8') || src.type === 'm3u8';

if (!isHlsUrl) {
return;
}

const hls = new Hls();

if (Hls.isSupported()) {
hls.loadSource(src.uri);
hls.attachMedia(nativeRef.current);
}

return () => {
hls.destroy();
};
}, [src]);

useMediaSession(src?.metadata, nativeRef, showNotificationControls);

return (
Expand Down
Loading