【问题标题】:Uncaught DOMException on route change hls.js React.js路由更改 hls.js React.js 时未捕获的 DOMException
【发布时间】:2020-03-12 21:59:49
【问题描述】:

我使用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>
  )
};

【问题讨论】:

  • 视频类型是什么?您尝试了其他视频源吗?
  • @AhedKabalan, mp4
  • 您是否尝试过其他视频源?我认为是视频格式的问题。
  • @AhedKabalan,嗯,让我检查一下

标签: javascript reactjs http-live-streaming hls.js plyr.js


【解决方案1】:

我修复了一个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>
  )
};

【讨论】:

    【解决方案2】:

    尝试将key 属性添加到您的&lt;video /&gt;

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

    我还看到许多其他人使用&lt;source /&gt; 标签。

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

    【讨论】:

    • 嗨,它工作得很好,但一旦 src 更改,它也会删除 plyr 样式:/
    • :( 哦不。嗯。可能值得一提的是,useEffect 仅在组件首次挂载时调用,而不是在状态更改时调用。也许您可以将其中的一些逻辑放在它之外? 对 plyr 了解不多
    • 谢谢你的帮助,Brandon,我找到了解决方案,因为我的useEffect,它不起作用,我会在20小时内给你奖励:)
    猜你喜欢
    • 2017-04-03
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    • 2017-07-08
    • 2019-07-22
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多