日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

React中插入視頻(video-react),自動播放

作者:前端學習007 更新時間: 2022-02-13 編程語言

一. 在react中引入

  1. 安裝插件
npm install –save video-react

2.在文件中引入

import {
  Player,
  ControlBar,
  PlayToggle, // PlayToggle 播放/暫停按鈕 若需禁止加 disabled
  ReplayControl, // 后退按鈕
  ForwardControl,  // 前進按鈕
  CurrentTimeDisplay,
  TimeDivider,
  PlaybackRateMenuButton,  // 倍速播放選項
  VolumeMenuButton
} from 'video-react';
import "video-react/dist/video-react.css"; // import css

二. 插件屬性
1.插件屬性地址

遇到的問題,視頻要求自動播放,設置autoPlay為true。在測試時候發現,切換Tab標簽的時候,不會自動播放。
解決方案:在更改數據的時候調用load方法。

 this.player.subscribeToStateChange(this.handleStateChange.bind(this));
 this.player.load();//  更改視頻源并重新加載視頻

部分代碼:


 this.player.subscribeToStateChange(this.handleStateChange.bind(this));
 this.player.load();
 
 handleStateChange(state, prevState) {
    this.setState({
      player: state,
      currentTime: state.currentTime,
      duration:state.duration,
    });
  }

界面

         <Player
            ref={c => {
              this.player = c;
            }}
            autoPlay='true'
            playsInline ='true'
            src={this.state.videoUrl}
            poster="https://video-react.js.org/assets/poster.png"
          >
            
            <ControlBar autoHide={false} disableDefaultControls={source == 0?false :true}>
              <ReplayControl seconds={10} order={1.1} />
              <PlayToggle />
              <CurrentTimeDisplay order={4.1} />
              <TimeDivider order={4.2} />
              <PlaybackRateMenuButton rates={[5, 2, 1.5, 1, 0.5]} order={7.1} />
              <VolumeMenuButton />
            </ControlBar>
          </Player>

原文鏈接:https://blog.csdn.net/weixin_44433499/article/details/115194493

欄目分類
最近更新