最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Adding subtitles in React Player? - Stack Overflow

programmeradmin1浏览0评论

i am trying to add subtitles in React Player ponent , Here is the code for react player ponent

  <ReactPlayer
          url={vidData.media[0]}
          controls={true}
          width={"100%"}
          height={"auto"}
          playing={playVid}
          muted={true}
          className="react-workout-player"
          config={{
            file: {
              attributes: {
                crossOrigin: "true",
              },
              tracks: [
                {
                  kind: "subtitles",
                  src: ".vtt",
                  srcLang: "en",
                  default: true,
                },
              ],
            },
          }}
        />

i cant understand what i am doing wrong as they are not appearing on the video. kindly help

i am trying to add subtitles in React Player ponent , Here is the code for react player ponent

  <ReactPlayer
          url={vidData.media[0]}
          controls={true}
          width={"100%"}
          height={"auto"}
          playing={playVid}
          muted={true}
          className="react-workout-player"
          config={{
            file: {
              attributes: {
                crossOrigin: "true",
              },
              tracks: [
                {
                  kind: "subtitles",
                  src: "https://prod.fitflexapp./files/captions/2021/11/18/23-b2j0oOsJ.vtt",
                  srcLang: "en",
                  default: true,
                },
              ],
            },
          }}
        />

i cant understand what i am doing wrong as they are not appearing on the video. kindly help

Share Improve this question edited Aug 5, 2022 at 5:20 Hamza Manan asked May 11, 2022 at 12:31 Hamza MananHamza Manan 5201 gold badge6 silver badges17 bronze badges 2
  • Does this Answer help you? – VC.One Commented May 11, 2022 at 23:42
  • this doesnt work either – Hamza Manan Commented May 12, 2022 at 6:28
Add a ment  | 

1 Answer 1

Reset to default 2

Create a State for an array For your subtitles

const [captions_arr, setCaptions] = useState([]);

yout captions_arr should have objects like this

  {kind: 'subtitles', src: 'subs/subtitles.en.vtt', srcLang: 'en', default: true},

Extract the part which you need from this array , I mapped it in a new Array

    var mySubtitle_arr= captions_arr.map((v) => ({
    kind: v.kind,
    src: v.src,
    srcLang: v.file_lang_code,
  }));

After Mapping pass this array to your react Player ponent

 <ReactPlayer
            config={{
              file: {
                attributes: {
                  crossOrigin: "true",
                },
                tracks: mySubtitle_arr,
              },
            }}
            url={'YOUR_VIDEO_URL'}
            controls={true}
            width={"100%"}
            height={"auto"}
            playing={'YOUR_PLAYING_STATE'}
            muted={true}
           
          />
发布评论

评论列表(0)

  1. 暂无评论