react-video hls.js 播放m3u8视频时,切换有延迟

发布于 2022-09-11 14:45:16 字数 1899 浏览 20 评论 0

recat-video hls.js开发视频播放页面时,加载m3u8,
切换视频更新resource后 重新播放了,但是时间较长的视频会播放之前的视频一段时间后才会切换到新的视频

HLSSource

    import React, { Component } from 'react';
import Hls from 'hls.js';

export default class HLSSource extends Component {
  constructor(props, context) {
    super(props, context);
    this.hls = new Hls();
  }

  componentDidMount() {
    // this.setHls()
  }
  componentWillReceiveProps() {
    
  }
  setHls = () => {
    console.log(this.hls)
    const { src, video } = this.props
    if (Hls.isSupported()) {
      this.hls.loadSource(src);
      this.hls.attachMedia(video);
    }
  }
  play = () => {
    const { video } = this.props
    this.hls.on(Hls.Events.MANIFEST_PARSED, () => {
      video.play();
    });
  }
  destroy = () => {
    if (this.hls) {
      this.hls.destroy()
    }
  }
  componentWillUnmount() {
    // destroy hls video source
    if (this.hls) {
      this.hls.destroy()
    }
  }

  render() {
    return (
      <source
        src={this.props.src}
        preload="none"
        type={this.props.type || 'application/x-mpegURL'}
      />
    );
  }
}

Player

  change = (lesson) => {
    
        const Player = this.refs.player
        this.setState({
            courseUrl: url
        }, () => {
            this.refs.hlsSource.setHls()
            // 更新resource后 重新播放,但是时间较长的视频会播放之前的视频一段时间后才会切换到新的视频
            this.refs.hlsSource.play() 
            // Player.load() 
        })
    }
    render ){
    return(
    <Player
            ref="player"
            playsInline
            autoPlay={false}
            className="video-play"
            startTime={this.state.startTime}>
            <HLSSource
                isVideoChild
                ref='hlsSource'
                src={this.state.courseUrl} />
        </Player>
        )}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢烬 2022-09-18 14:45:16

推荐一个播放器

dplayer(http://dplayer.js.org/#/)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文