react-native-video在IOS上的自动重复问题

bjg7j2ky  于 12个月前  发布在  iOS
关注(0)|答案(2)|浏览(132)

我正在开发的React本地应用程序,显示视频。我用的是react-native-video包。当视频页面打开时,视频将自动开始一次,视频结束后不应重复。它在Android上运行良好,但在iOS上,视频自动从头开始,并不断重复。这是我的代码停止视频后,视频结束:

const onEnd = () => {
   setState({ ...state, play: false, showControls: true });
   if (videoRef.current) {
     videoRef.current.seek(0);
   }
  };

我该如何解决这个问题?

bxgwgixi

bxgwgixi1#

我通过删除这行代码解决了这个问题。保持当前视频时间在最大持续时间,不要设置为0,这将使无限重复条件。

if (videoRef.current) {
 videoRef.current.seek(0);
}
xfb7svmp

xfb7svmp2#

// Create a state alongside play_pause --> 
        const [videoStarted, setVideoStarted] = useState(false);

    // update play_pause func -->
        const _handlePlayPauseClick = () => {
            // update state and call the seek function here instead 
            // of calling in the end of video.
            setVideoPaused(!videoPaused);
            setVideoStarted(true);  
            if(!videoStarted){
                _seekVideo(0)
            }
            // <---
        };

    // update video_end func -->
        const _handleEnd = () => {
            setVideoPaused(true);
            setVideoStarted(false);  // --> add it
            // _seekVideo(0);  // --> remove it
        };
    
        const _seekVideo = (timeInSeconds) => {
            if (videoRef.current) {
                videoRef.current.seek(timeInSeconds);
                setProgress(timeInSeconds);
            }
        };

   // Video component -->
            <Video
                ref={videoRef}
                source={{ uri: videoUrl }}
                resizeMode="cover"
                controls={false}
                paused={videoPaused}
                style={{
                    position: 'absolute',
                    top: 0,
                    left: 0,
                    bottom: 0,
                    right: 0,
                }}
                onLoad={_handleLoad}
                onProgress={_handleProgress}
                onEnd={_handleEnd}
            />

你现在可以走了。希望这将有助于

相关问题