Open duthanhduoc opened 10 months ago
Dưới đây là hướng dẫn fix lỗi streaming HLS.
Đầu tiên thì xóa @vidstack/react
và vidstack
trong source FE đi.
Tiếp theo mở file index.html
trong source front-end xóa hoặc comment cái dòng này đi
<meta
http-equiv="Content-Security-Policy"
content="default-src *; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'; img-src * data: 'unsafe-inline'; connect-src * 'unsafe-inline'; frame-src *;"
/>
Tiếp theo chúng ta cài hls.js
và plyr
vào source FE.
npm i hls.js plyr
Mở file Home.jsx lên thêm như sau
import { useLayoutEffect, useRef } from "react";
//...
export default function Home() {
//...
const video = useRef();
const playerInstance = useRef();
useLayoutEffect(() => {
const source =
"http://localhost:4000/static/video-hls/j4Y-3iUyKlbdk6LYsImVW/master.m3u8";
const hls = new Hls();
hls.loadSource(source);
window.hls = hls;
hls.on(Hls.Events.MANIFEST_PARSED, function () {
const availableQualities = hls.levels.map((l) => l.height);
playerInstance.current = new Plyr(video.current, {
quality: {
default: availableQualities[0],
options: availableQualities,
forced: true,
onChange: (e) => updateQuality(e),
},
});
});
hls.attachMedia(video.current);
return () => {
playerInstance.current.destroy();
};
}, [playerInstance, video]);
//...
return (
<>
<video
id="player"
ref={video}
className="video-js"
autoPlay
preload="auto"
></video>
</>
Có một số lỗi liên quan đến sự khác nhau giữa Mac / Window / linux nên chúng ta sẽ dùng thư viện để chuẩn hóa mấy cái này. Thư viện ở đây anh dùng là
zx
để thực hiện command.Đầu tiên vào
tsconfig.json
sửa lại như sauSau đó Em cài
npm i zx
vào dự án. Đây là thư viện build trên es module.Tiếp theo em sửa lại nội dung file
video.ts
như sauCùng với đó anh thấy bên file
file.ts
của em tại functionhandleUploadVideo
em đang dùng nanoId nhưng thư viện này là es module, vậy nên hãy dùng lại cho đúng bằng cách