繁体   English   中英

路由更改 hls.js React.js 时未捕获的 DOMException

[英]Uncaught DOMException on route change hls.js React.js

我使用plyr.js创建视频播放器。
遇到hls.js错误:

Uncaught DOMException: Failed to read the 'buffered' property from 'SourceBuffer': 
This SourceBuffer has been removed from the parent media source.

当我在路线更改时更改视频src时会发生这种情况。

我的播放器:

import React from 'react'
import HLS from 'hls.js'
import Plyr from 'plyr'

const Player = ({src}) => {

  const [player, setPlayer] = useState(null);
  const video = useRef(null);

  useEffect(() => {

    const node = video.current;

    // Thought it would help, but no
    const destroy = () => {
      if (window.hls) {
        window.hls.stopLoad();
        window.hls.detachMedia();
        window.hls.destroy();
      }
    };

    if (node) {
      if(!player) setPlayer(new Plyr(node, {captions: {active: true, update: true}}))
      if (HLS.isSupported()) {
        destroy();
        window.hls = new HLS();
        window.hls.loadSource(src);
        window.hls.attachMedia(node);
      } else node.src = src;
    }

    }, [src, player]);

  return (
    <div>
      <video ref={video} src={src} controls/>
    </div>
  )
};

我修复了一个useEffect所以它现在可以工作了:

import React , { useEffect, useRef, useState,context }  from 'react'
import HLS from 'hls.js'
import Plyr from 'plyr'

const destroyHLS = () => {
  window.hls.stopLoad();
  window.hls.detachMedia();
  window.hls.destroy();
};


const Player = ({src}) => {

  const [player, setPlayer] = useState(null);
  const video = useRef(null);

  useEffect(() => setPlayer(new Plyr(video.current, context)), [src]);

  useEffect(() => {

    let ignore = false;

    if (HLS.isSupported()) {
      if (window.hls) destroyHLS();
      window.hls = new HLS();
      window.hls.loadSource(src);
      window.hls.attachMedia(video.current);
    } else video.current.src = src;

    if (ignore) player.destroy();

    return () => {
      ignore = true;
    };
  }, [player, src]);

  return (
    <div>
      <video ref={video} src={src} controls/>
    </div>
  )
};

尝试向您的<video />添加一个key道具。

<video ref={video} src={src} key={src} controls />

我还看到许多其他人使用<source />标签。

<video ref={video} key={src}>
  <source src={src} />
</video>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM