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

Added HLS Instance accessibility support #42

Open
wants to merge 1 commit 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
49 changes: 48 additions & 1 deletion example/App/App.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Hls from 'hls.js';
import React, { useState, useRef } from 'react';

import HlsPlayer from '../../src';
Expand All @@ -9,6 +10,9 @@ function App() {
'https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8'
);
const [destroy, setDestroy] = useState(false);
const [HLSInstance, setHLSInstance] = useState<Hls>();
const [displayHLSPrperties, setDisplayHLSPrperties] = useState(false)
const [selectedQuality, setSelectedQuality] = useState<number>();

function _handleEnter(e: React.KeyboardEvent) {
if (e.keyCode === 13) {
Expand All @@ -28,6 +32,19 @@ function App() {
}
}

function _handleHLSInstanceClick() {
setDisplayHLSPrperties(!displayHLSPrperties)
}

function getHLSInstance(hlsInstance: Hls) {
setHLSInstance(hlsInstance);
}

function handleQuality(e: React.ChangeEvent<HTMLSelectElement>) {
setSelectedQuality(Number(e.target.value));
if(HLSInstance) HLSInstance.currentLevel = Number(e.target.value);
}

return (
<div>
<div
Expand Down Expand Up @@ -68,6 +85,7 @@ function App() {
autoPlay
playerRef={playerRef}
src={hlsUrl}
getHLSInstance={getHLSInstance}
/>
) : null}

Expand All @@ -90,7 +108,36 @@ function App() {
>
Toggle Controls (via custom ref)
</button>
</div>
<button
style={{
padding: '5px 10px',
}}
onClick={_handleHLSInstanceClick}
>
Get HLS Instance
</button>

{displayHLSPrperties && (
<div
style={{
padding: "5px 10px",
border: "1px solid black",
margin: "15px 15px",
}}
>
<h3>HLS Details</h3>
<p>Total Levels: {HLSInstance?.levels?.length}</p>
<div>
<p>Select Quoality</p>
<select value={selectedQuality} defaultValue={`${HLSInstance?.currentLevel}`} onChange={handleQuality}>
{HLSInstance?.levels?.map((level, index) => {
return <option value={`${index}`}>{level.bitrate}</option>
})}
</select>
</div>
</div>
)}
</div>
);
}

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

14 changes: 13 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import React, { useEffect, RefObject } from 'react';
import React, { useEffect, RefObject, useState } from 'react';
import Hls, { Config } from 'hls.js';

export interface HlsPlayerProps
extends React.VideoHTMLAttributes<HTMLVideoElement> {
hlsConfig?: Config;
playerRef: RefObject<HTMLVideoElement>;
src: string;
getHLSInstance?: (HLSInstance: Hls) => void;
}

function ReactHlsPlayer({
hlsConfig,
playerRef = React.createRef<HTMLVideoElement>(),
src,
autoPlay,
getHLSInstance,
...props
}: HlsPlayerProps) {
const [hlsInstance, setHlsInstance] = useState<Hls>();

useEffect(() => {
let hls: Hls;

Expand Down Expand Up @@ -64,6 +68,7 @@ function ReactHlsPlayer({
}
});

setHlsInstance(newHls);
hls = newHls;
}

Expand All @@ -79,6 +84,13 @@ function ReactHlsPlayer({
};
}, [autoPlay, hlsConfig, playerRef, src]);

useEffect(() => {
if(getHLSInstance && hlsInstance){
getHLSInstance(hlsInstance)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [hlsInstance])

// If Media Source is supported, use HLS.js to play video
if (Hls.isSupported()) return <video ref={playerRef} {...props} />;

Expand Down