禁用 react-player 中的寻找功能未生效。

huangapple go评论49阅读模式
英文:

Disabling seeking functionality in react-player not working

问题

我正在尝试禁用 react-player 中的跳跃播放,以便用户必须完整观看视频,无法跳过。以下是我想出的代码:

import ReactPlayer from "react-player";
import { useRef } from "react";

const MyLessons = () => {
  const playerRef = useRef(null);
  const prevPlayed = useRef(null);

  const handleSeek = () => {
    if (prevPlayed.current) {
      playerRef.current.seekTo(prevPlayed.current.playedSeconds, 'seconds');
    }
  };

  const handleProgress = (state) => {
    prevPlayed.current = state;
  };

  return (
    <div className="app">
      <main className="content">
        <div className="player-wrapper">
          <ReactPlayer
            ref={playerRef}
            className="react-player"
            url="lessons/testmp4.MP4"
            width="100%"
            height="100%"
            controls
            config={{ file: { attributes: { controlsList: 'nodownload' } } }}
            onProgress={handleProgress}
            onSeek={handleSeek}
          />
        </div>
      </main>
    </div>
  );
};

export default MyLessons;

当视频开始播放并且用户尝试跳过或定位到视频的特定部分时,视频会返回到之前的位置,但视频会暂停,并且之后无法恢复播放。

另外,在控件(ReactPlayer 的左下角)中,播放按钮会不断地从播放切换为恢复按钮,似乎陷入循环尝试继续播放,但有些东西阻止了它。

有什么建议可以解决这个问题吗?

英文:

I'm trying to disable seeking in react-player, so the user needs to watch full video without any skipping. Here is the code I came up with:

import ReactPlayer from &quot;react-player&quot;;
import { useRef } from &quot;react&quot;;

const MyLessons = () =&gt; {
const playerRef = useRef(null);
const prevPlayed = useRef(null);

const handleSeek = () =&gt; {
  if (prevPlayed.current) {
    playerRef.current.seekTo(prevPlayed.current.playedSeconds, &#39;seconds&#39;);
  }
};

const handleProgress = (state) =&gt; {
  prevPlayed.current = state;
};

return (
  &lt;div className=&quot;app&quot;&gt;
    &lt;main className=&quot;content&quot;&gt;
      &lt;div className=&quot;player-wrapper&quot;&gt;
        &lt;ReactPlayer
          ref={playerRef}
          className=&quot;react-player&quot;
          url=&quot;lessons/testmp4.MP4&quot;
          width=&quot;100%&quot;
          height=&quot;100%&quot;
          controls
          config={{ file: { attributes: { controlsList: &#39;nodownload&#39; } } }}
          onProgress={handleProgress}
          onSeek={handleSeek}
        /&gt;
      &lt;/div&gt;
    &lt;/main&gt;
  &lt;/div&gt;
 );
};

export default MyLessons;

When the video starts and user try to skip or seek to specific part of video, the video is reverted to previous point, but the video is paused and can not be resumed afterwards.

Also, in controls (bottom left corner of ReactPlayer) play button switches from play to resume button non-stop, seems like it is in loop trying to continue but something is stopping it.

Any suggestions how this can be fixed?

答案1

得分: 0

这是您提供的React代码的翻译部分:

看起来使用ReactPlayer不可能做到这一点。最终禁用了控件并创建了自定义的播放/静音按钮:

const Lesson = () => {
  const [isPlaying, setIsPlaying] = useState(true);
  const [isMuted, setIsMuted] = useState(false);

  const handlePlayPause = () => {
    setIsPlaying(!isPlaying);
  };

  const handleMuteUnmute = () => {
    setIsMuted(!isMuted);
  };

  return (
    <div className="app-video">
      <ReactPlayer
        className="react-player"
        url="lessons/test1mp4.MP4"
        width="100%"
        height="100%"
        playing={isPlaying}
        volume={isMuted ? 0 : 0.5}
        controls={false}
        onEnded={() => {
          console.log("ended");
        }}
        config={{
          file: {
            attributes: {
              playsInline: true,
            },
          },
        }}
      />
    </div>
    <div className="bottom-panel">
      <Button
        className={`control-button ${isPlaying ? "pause" : "play"}`}
        onClick={handlePlayPause}
        startIcon={isPlaying ? <PauseIcon /> : <PlayArrowIcon />}
      >
        {isPlaying ? "Pause" : "Play"}
      </Button>
      <Button
        className={`control-button ${isMuted ? "unmute" : "mute"}`}
        onClick={handleMuteUnmute}
        startIcon={isMuted ? <VolumeOffIcon /> : <VolumeUpIcon />}
      >
        {isMuted ? "Unmute" : "Mute"}
      </Button>
    </div>
  );
};

export default Lesson;
英文:

Seems like it's not possible with ReactPlayer to do that. Ended up with disabling controls and creating custom play/mute buttons:

const Lesson = () =&gt; {
const [isPlaying, setIsPlaying] = useState(true);
const [isMuted, setIsMuted] = useState(false);
const handlePlayPause = () =&gt; {
setIsPlaying(!isPlaying);
};
const handleMuteUnmute = () =&gt; {
setIsMuted(!isMuted);
};
return (
&lt;div className=&quot;app-video&quot;&gt;
&lt;ReactPlayer
className=&quot;react-player&quot;
url=&quot;lessons/test1mp4.MP4&quot;
width=&quot;100%&quot;
height=&quot;100%&quot;
playing={isPlaying}
volume={isMuted ? 0 : 0.5}
controls={false}
onEnded={() =&gt; {
console.log(&quot;ended&quot;);
}}
config={{
file: {
attributes: {
playsInline: true,
},
},
}}
/&gt;
&lt;/div&gt;
&lt;div className=&quot;bottom-panel&quot;&gt;
&lt;Button
className={`control-button ${isPlaying ? &quot;pause&quot; : &quot;play&quot;}`}
onClick={handlePlayPause}
startIcon={isPlaying ? &lt;PauseIcon /&gt; : &lt;PlayArrowIcon /&gt;}
&gt;
{isPlaying ? &quot;Pause&quot; : &quot;Play&quot;}
&lt;/Button&gt;
&lt;Button
className={`control-button ${isMuted ? &quot;unmute&quot; : &quot;mute&quot;}`}
onClick={handleMuteUnmute}
startIcon={isMuted ? &lt;VolumeOffIcon /&gt; : &lt;VolumeUpIcon /&gt;}
&gt;
{isMuted ? &quot;Unmute&quot; : &quot;Mute&quot;}
&lt;/Button&gt;
&lt;/div&gt;
);
};
export default Lesson;

答案2

得分: 0

以下是您要翻译的代码部分:

const videoRef = useRef<ReactPlayer>(null)
const [played, setPlayed] = useState(0)

return (
     <ReactPlayer
          config={{
                file: { attributes: { controlsList: 'nodownload' } },
          }}
          onContextMenu={(e) => e.preventDefault()}
          url={video.link} // Replace with your video URL
          controls // Enable video player controls
          width="100%"
          height="auto"
          ref={videoRef}
          onProgress={() => {
                videoRef.current.getCurrentTime() >= played &&
                setPlayed(videoRef.current.getCurrentTime())
          }}
          onSeek={() => {
                videoRef.current.getCurrentTime() > played &&
                videoRef.current.seekTo(played)
          }}
     />
)

希望这对您有所帮助。如果您需要任何其他翻译,请告诉我。

英文:

A thing here I believe that can be useful rather than disabling your controls:

const videoRef = useRef&lt;ReactPlayer&gt;(null)
const [played, setPlayed] = useState(0)

return (
     &lt;ReactPlayer
          config={{
                file: { attributes: { controlsList: &#39;nodownload&#39; } },
          }}
          onContextMenu={(e) =&gt; e.preventDefault()}
          url={video.link} // Replace with your video URL
          controls // Enable video player controls
          width=&quot;100%&quot;
          height=&quot;auto&quot;
          ref={videoRef}
          onProgress={() =&gt; {
                videoRef.current.getCurrentTime() &gt;= played &amp;&amp;
                setPlayed(videoRef.current.getCurrentTime())
          }}
          onSeek={() =&gt; {
                videoRef.current.getCurrentTime() &gt; played &amp;&amp;
                videoRef.current.seekTo(played)
          }}
     /&gt;
)

So, whenever the user tries to seek future frames, their playback is redirected to what's in the played state. They can go back in the previous frames but going to future frames would put them back to where the farthest seconds they have progressed.

huangapple
  • 本文由 发表于 2023年5月11日 19:51:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76227343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定