【问题标题】:Pause video on bootstrap carousel slide change在引导轮播幻灯片更改上暂停视频
【发布时间】:2021-07-17 15:31:33
【问题描述】:

我有一组视频,这些视频由嵌入在模式对话框中的 Bootstrap 轮播显示。显示模态时视频不会自动开始播放,但您需要单击它,它将开始播放。我的问题是幻灯片更改当前视频播放不会暂停。所以这就是我需要实现的,当用户更改幻灯片(前后)时暂停当前视频。我怎样才能做到这一点? 顺便说一句,我正在使用 React Js。

非常重视任何帮助。谢谢。✌️

下面是我的视频轮播组件。

import React, { Component } from "react";
import 'bootstrap/dist/css/bootstrap.min.css';
import Carousel from 'react-bootstrap/Carousel';
import "../carousel.css";

export default class VideoCarousel extends Component {
  constructor(props) {
    super(props);
    this.videoRef = React.createRef();
    // this.state = {
    //   index: 0,
    //   isPlaying: false
    // }
  }
render(){
return(
      <div>
      <Carousel activeIndex={this.index} onSelect={this.handleChange} interval={null}  className="carousel">
          {this.props.items.map((item, index) => {
              return (
                  <Carousel.Item key={index}>
                  <video 
                    ref = {this.videoRef} 
                    className="videoItem" 
                    controls="controls"
                  >
                    <source src={item.src} type="video/mp4"/>
                  </video>
                  <Carousel.Caption>
                    {/* <h2>{item.title}</h2> */}
                  </Carousel.Caption>
                  </Carousel.Item>
              );
          })}
      </Carousel>
      </div>
    )
  }
}

【问题讨论】:

    标签: reactjs bootstrap-4 modal-dialog carousel


    【解决方案1】:

    问题

    您对所有项目使用相同的参考。运行地图函数后 this.videoRef 将等于数组的最后一个元素。

    解决方案 1:将引用保存在数组中

    创建由空数组初始化的 ref:
     this.videoRef = React.createRef([]);
    

    遍历元素并根据索引值分配引用:

    <Carousel activeIndex={this.index} onSelect={this.handleChange} interval={null}  className="carousel">
      {this.props.items.map((item, index) => {
         return (
              <Carousel.Item key={index}>
                <video 
                  ref = {el => this.videoRef.current[index] = el} 
                  ...
                >
                  ... 
              </Carousel.Item>)
       })}
    </Carousel>
    

    要暂停视频,请使用特定位置的元素引用:

    this.videoRef.current[index].pause();
    

    解决方案 2:为轮播项目创建一个组件

    您可以处理componentDidMount/componentWillUnmount上的控件功能

     this.videoRef = React.createRef(null);
    

    分配参考:

    export default class VideoCarouselItem extends Component {
      constructor(props) {
        super(props);
        this.videoRef = React.createRef();
      }
     render(){
        <Carousel.Item key={this.props.key}>
           <video 
             ref = {this.videoRef} 
             ...
           >             
        </Carousel.Item>
     }
    }
    

    然后通过属性传递你需要的信息。

    【讨论】:

    • 感谢您的回答。不幸的是,我仍然找不到这样做的方法。当我单击应该出现在屏幕上的视频轮播的视频按钮时出现此错误。 “TypeError:无法读取 null VideoCarousel.handleChange src/components/VideoCarousel.js:19 16 | console.log("indexx::", index); > 19 | this.videoRef.current[index] 的属性 '1'。 pause(); | ^ 20 | 21 | }); 22 | " 我不知道我哪里出错了。你能帮我提琴吗?非常感谢。无论如何。
    • 或此错误:“TypeError:无法设置 null ref src/components/VideoCarousel.js:34 31 | 32 |
    • @AnadDana 我用另一个解决方案更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-30
    • 2021-07-29
    • 2012-07-02
    • 2021-12-16
    相关资源
    最近更新 更多