gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:06:35 24 4
gpt4 key购买 nike

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

关于javascript - 路由更改时未捕获 DOMException hls.js React.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58899212/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com