i am trying to add subtitles in React Player component , Here is the code for react player component
JavaScript
x
25
25
1
<ReactPlayer
2
url={vidData.media[0]}
3
controls={true}
4
width={"100%"}
5
height={"auto"}
6
playing={playVid}
7
muted={true}
8
className="react-workout-player"
9
config={{
10
file: {
11
attributes: {
12
crossOrigin: "true",
13
},
14
tracks: [
15
{
16
kind: "subtitles",
17
src: "https://prod.fitflexapp.com/files/captions/2021/11/18/23-b2j0oOsJ.vtt",
18
srcLang: "en",
19
default: true,
20
},
21
],
22
},
23
}}
24
/>
25
i cant understand what i am doing wrong as they are not appearing on the video. kindly help
Advertisement
Answer
Create a State for an array For your subtitles
JavaScript
1
2
1
const [captions_arr, setCaptions] = useState([]);
2
yout captions_arr should have objects like this
JavaScript
1
2
1
{kind: 'subtitles', src: 'subs/subtitles.en.vtt', srcLang: 'en', default: true},
2
Extract the part which you need from this array , I mapped it in a new Array
JavaScript
1
6
1
var mySubtitle_arr= captions_arr.map((v) => ({
2
kind: v.kind,
3
src: v.src,
4
srcLang: v.file_lang_code,
5
}));
6
After Mapping pass this array to your react Player component
JavaScript
1
18
18
1
<ReactPlayer
2
config={{
3
file: {
4
attributes: {
5
crossOrigin: "true",
6
},
7
tracks: mySubtitle_arr,
8
},
9
}}
10
url={'YOUR_VIDEO_URL'}
11
controls={true}
12
width={"100%"}
13
height={"auto"}
14
playing={'YOUR_PLAYING_STATE'}
15
muted={true}
16
17
/>
18