如何正确在React中使用Hls.js?

huangapple go评论70阅读模式
英文:

How to use Hls.js with React properly?

问题

我正在使用Hls.js构建一个用于视频播放器的React组件。我正在使用React的Refs,并在渲染后调用useEffect来获取视频元素。问题是我一直在收到一个错误。

以下是您的代码中翻译后的部分:

import Hls from "hls.js";
import { useEffect, useRef } from "react";

export default function VideoPlayer({ data, className = '', ...props }) {
  const videoRef = useRef();

  useEffect(() => {
    const hls = new Hls({
      "debug": true
    });

    if (Hls.isSupported()) {
      hls.log = true;
      hls.loadSource(data.src);
      console.log(videoRef);
      hls.attachMedia(videoRef.current);
      hls.on(Hls.Events.ERROR, (err) => {
        console.log(err);
      });

    } else {
      console.log('load');
    }
  }, [])

  return (
    <video
      ref={videoRef}
      controls
      {...props}
      src={data.src}
      className={className}
    />
  )
}

这是控制台中的输出:

[log] > 调试日志已启用,hls.js版本为1.4.9-0.canary.9343的"Hls实例"中

VideoPlayer.jsx:16 {current: video}current: nullsrc: "blob:http://192.168.0.205/c68f5827-3d23-4656-9479-c64f7f9b2e51"[[Prototype]]: Object
VideoPlayer.jsx:17 [log] > attachMedia
react-dom.development.js:22839

Uncaught TypeError: media.addEventListener不是一个函数
在BufferController.onMediaAttaching (buffer-controller.ts:181:13)
在EventEmitter2.emit (index.js:203:33)
在Hls.emit (hls.ts:288:26)
在Hls.trigger (hls.ts:296:19)
在Hls.attachMedia (hls.ts:360:10)
在VideoPlayer.jsx:17:11
在commitHookEffectListMount (react-dom.development.js:23150:26)
在commitPassiveMountOnFiber (react-dom.development.js:24926:13)
在commitPassiveMountEffects_complete (react-dom.development.js:24891:9)
在commitPassiveMountEffects_begin (react-dom.development.js:24878:7)

控制台错误截图

由于Hls.js需要在渲染后加载,我使用了useEffect,但仍然遇到了问题。我已经检查了其他建议,但问题仍然存在。

如何在React中使用hls.js

英文:

I am building a react for a video player using Hls.js. I am using React Refs and calling useEffect to get the video element after it is rendard. The issue is I keep getting an error.

import Hls from &quot;hls.js&quot;;
import { useEffect, useRef } from &quot;react&quot;;

export default function VideoPlayer({ data, className = &#39;&#39;, ...props }) {
  const videoRef = useRef();

  useEffect(()=&gt;{
    const hls = new Hls({
      &quot;debug&quot;: true
    });

    if (Hls.isSupported()) {
      hls.log = true;
      hls.loadSource = data.src;
      console.log(videoRef)
      hls.attachMedia(videoRef)
      hls.on(Hls.Events.ERROR, (err) =&gt; {
        console.log(err)
      });

    } else {
      console.log(&#39;load&#39;)
    }
  },[])

  return (
    &lt;video
      ref={videoRef}
      controls
      {...props}
      src={data.src}
      className={className}
    /&gt;
  )
}

This is the output I get in the console.

> [log] > Debug logs enabled for "Hls instance" in hls.js version 1.4.9-0.canary.9343

> VideoPlayer.jsx:16 {current: video}current: nullsrc: "blob:http://192.168.0.205/c68f5827-3d23-4656-9479-c64f7f9b2e51"[[Prototype]]: Object
> VideoPlayer.jsx:17 [log] > attachMedia
> react-dom.development.js:22839

> Uncaught TypeError: media.addEventListener is not a function
> at BufferController.onMediaAttaching (buffer-controller.ts:181:13)
> at EventEmitter2.emit (index.js:203:33)
> at Hls.emit (hls.ts:288:26)
> at Hls.trigger (hls.ts:296:19)
> at Hls.attachMedia (hls.ts:360:10)
> at VideoPlayer.jsx:17:11
> at commitHookEffectListMount (react-dom.development.js:23150:26)
> at commitPassiveMountOnFiber (react-dom.development.js:24926:13)
> at commitPassiveMountEffects_complete (react-dom.development.js:24891:9)
> at commitPassiveMountEffects_begin (react-dom.development.js:24878:7)

Screenshot of Error in Console

Seeing that the Hls.js needs to load after the redering, I used the useEffect but still have an issue.

I have reviewed the other suggestion already and still have the issue.

How to use hls.js with react

答案1

得分: 0

我能够找出我的错误。我没有抓取当前 ref。

import Hls from "hls.js";
import { useEffect, useRef } from "react";

export default function VideoPlayer({ data, className = '', ...props }) {
  const videoRef = useRef();

  useEffect(() => {
    const hls = new Hls({
      "debug": true
    });

    if (Hls.isSupported()) {
      hls.log = true;
      hls.loadSource = data.src;
      hls.attachMedia(videoRef.current)
      hls.on(Hls.Events.ERROR, (err) => {
        console.log(err)
      });

    } else {
      console.log('load')
    }
  }, [data])

  return (
    <video
      ref={videoRef}
      controls
      {...props}
      src={data.src}
      className={className}
    />
  )
}

我希望这对其他人有所帮助。这是一个简短的帖子,但在喝咖啡后我能够找出解决方法...

英文:

I was able to figure out my mistake. I wasn't grabbing the current ref

import Hls from &quot;hls.js&quot;;
import { useEffect, useRef } from &quot;react&quot;;

export default function VideoPlayer({ data, className = &#39;&#39;, ...props }) {
  const videoRef = useRef();

  useEffect(()=&gt;{
    const hls = new Hls({
      &quot;debug&quot;: true
    });

    if (Hls.isSupported()) {
      hls.log = true;
      hls.loadSource = data.src;
      hls.attachMedia(videoRef.current)
      hls.on(Hls.Events.ERROR, (err) =&gt; {
        console.log(err)
      });

    } else {
      console.log(&#39;load&#39;)
    }
  },[data])

  return (
    &lt;video
      ref={videoRef}
      controls
      {...props}
      src={data.src}
      className={className}
    /&gt;
  )
}

I hope this helps anyone else. This was a short post but I was able to figure out after making some coffee...

huangapple
  • 本文由 发表于 2023年7月7日 04:12:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632259.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定