javascript 播放视频时出错,无法读取未定义的属性(阅读'play')

lokaqttq  于 2023-03-28  发布在  Java
关注(0)|答案(1)|浏览(295)

我有一个视频库,里面有正在Map的视频。我一次播放一个视频时遇到了问题。相反,我得到了错误,Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'play')。我认为这是因为函数是asnyc,我将播放函数更改为async/await。仍然不起作用。这种格式适用于一个视频,但不能多个。
我希望能够播放视频

export default function VideoGallery() {
  const [videoData, setVideoData] = useState([]);
  const [showControls, setShowControls] = useState(false);
  const vidRef = useRef();

  //bunch of code

  const playVideo = async (e) => {
    await vidRef.current.play();
    setShowControls(true);
  };

  return (
    <div className="relative w-full p-1 group">
      <div className=" flex flex-wrap -m-1 md:-m-2 ">
        {videoData.map((data) => {
          const videoURL = data.videoURL.replace(
            "https://firebasestorage.googleapis.com",
            ""
          );
          return (
            <>
              <div
                key={data.videoURL}
                className="relative flex w-full md:w-1/3 sm:w-1/2 flex-wrap group"
              >
                <div className="relative w-full p-1 group">
                  <video
                    src=...
                    preload="metadata"
                    volume="true"
                    controls={showControls ? "controls" : ""}
                    controlsList="nodownload"
                    refs={vidRef}
                  />
                  <div className="absolute top-1/2 left-1/2 -translate-y-1/2 -translate-x-1/2">
                    <BsPlayCircle
                      className="text-[#fa9204] text-6xl cursor-pointer"
                      onClick={playVideo}
                      refs={vidRef}
                    />
                  </div>
                </div>
              </div>
            </>
          );
        })}
      </div>
    </div>
  );
}
eni9jsuy

eni9jsuy1#

  • 此格式适用于一个视频,但不能用于多个视频。* 显然它只适用于一个视频,因为您正在循环中分配ref。因此ref将在每次迭代中覆盖。

此外,它不起作用,因为你在一次迭代中将引用分配给多个元素。视频元素和播放按钮元素。
请检查下面的解决方案,我做了一些小的修改。现在ref将是数组,我们可以使用索引访问它。希望这个解决方案有帮助!

import { useState, useRef } from "react";
import "./styles.css";

export default function VideoGallery() {
  const [videoData, setVideoData] = useState([]);
  const [showControls, setShowControls] = useState(false);
  const vidRef = useRef([]);

  const playVideo = (index) => {
    vidRef.current[index].play();
    setShowControls(true);
  };

  return (
    <div className="relative w-full p-1 group">
      <div className=" flex flex-wrap -m-1 md:-m-2 ">
        {videoData.map((data, i) => (
          <div
            key={data.videoURL}
            className="relative flex w-full md:w-1/3 sm:w-1/2 flex-wrap group"
          >
            <div className="relative w-full p-1 group">
              <video
                width="250px"
                src={data.videoURL}
                preload="metadata"
                volume="true"
                controls={showControls ? "controls" : ""}
                controlsList="nodownload"
                ref={el => vidRef.current.push(el)}
              />
              <div className="absolute top-1/2 left-1/2 -translate-y-1/2 -translate-x-1/2">
                <BsPlayCircle
                  className="text-[#fa9204] text-6xl cursor-pointer"
                  onClick={() => playVideo(i)}
                />
              </div>
            </div>
          </div>
        ))}
      </div>
    </div>
  );
}

相关问题