I am trying to load heavy video file (around 300mb in tag in html. I tried setting the source as a URL but it lags and cause performance issue in chrome. I tried using the Media Source API in my react Application. But I am getting errors. Please suggest a way I can load heavy videos smoothly into browser in React.js.
Here is the component I created for implementing Media Source API:
import React, { useEffect, useRef } from 'react';
const VideoStream = (props) => {
const videoRef = useRef(null);
useEffect(() => {
const video = videoRef.current;
if (window.MediaSource) {
const mediaSource = new MediaSource();
video.src = URL.createObjectURL(mediaSource);
mediaSource.addEventListener('sourceopen', handleSourceOpen);
} else {
console.error('MediaSource API is not supported.');
}
}, []);
const handleSourceOpen = async (event) => {
const mediaSource = event.target;
console.log(videoRef.current)
const sourceBuffer = mediaSource.addSourceBuffer('video/mp4; codecs="avc1.42E01E, mp4a.40.2"');
// Fetch and append media segments progressively
const segmentUrl = props.src;
const response = await fetch(segmentUrl);
const segmentData = await response.arrayBuffer();
console.log(segmentData)
sourceBuffer.appendBuffer(segmentData);
sourceBuffer.addEventListener('updateend', () => {
if (!sourceBuffer.updating && mediaSource.readyState === 'open') {
mediaSource.endOfStream();
}
});
videoRef.current.play();
};
return <video ref={videoRef} controls />;
};
export default VideoStream;
The error I am getting is:
GET blob:http://localhost:3000/b54e8a3a-769e-40a8-8cd3-3a49cf14946a net::ERR_FILE_NOT_FOUND