简体   繁体   English

在 React-Native FlatList 中播放和暂停视频或仅播放一个视频可见

[英]play and pause videos in React-Native FlatList or Play video only one Video visible

Hi i am doing video Gallery app like TikTok using React-Native with FlatList , While i am doing the app i got the error with videos play and pause options.嗨,我正在使用 React-Native 和 FlatList 做像 TikTok 这样的视频库应用程序,当我在做这个应用程序时,我遇到了视频播放和暂停选项的错误。 When i open the app all videos are playing at a time in the background but i want to play the only one video which is visible one the screen or in the view point.当我打开应用程序时,所有视频都在后台播放,但我想播放唯一一个在屏幕或视点中可见的视频。 So once check the below code and give me any better suggestions ..所以一旦检查下面的代码并给我任何更好的建议..


import SegmentedControlTab from 'react-native-segmented-control-tab';
import Video from 'react-native-video';
import VideoPlayer from 'react-native-video-controls';

import MediaControls, { PLAYER_STATES } from 'react-native-media-controls';

import axios from 'axios';

export default class HomeScreen extends Component {
  _isMounted = false;
  constructor(Props) {
    super(Props);
    this.state = {
      error: null,
      isLoading: false,

      playing: false,
      currentTime: 10,
      duration: 0,
      isFullScreen: false,
      isLoading: true,
      paused: true,
      playerState: PLAYER_STATES.PLAYING,
      screenType: 'content',

    };

    this.viewabilityConfig = {
      waitForInteraction: true,
      viewAreaCoveragePercentThreshold: 95
    }


  }
  onViewableItemsChanged = ({ viewableItems, changed }) => {
    console.log("Visible items are", viewableItems);
    console.log("Changed in this iteration", changed);
  }




 render() {
    const { duration, paused, overlay } = this.state

    return (
      <View style={styles.container}>        

         <FlatList style={styles.list}
                data={this.state.following_Posts}
                // keyExtractor={(item, index) => index.toString()}
                keyExtractor={(data_Posts, index) => {
                  return data_Posts.id.toString();
                }}
                ItemSeparatorComponent={() => { return (<View style={styles.separator} />) }}

               // viewabilityConfig={{ viewAreaCoveragePercentThreshold: 95 }}
              viewabilityConfig={this.viewabilityConfig}

              onViewableItemsChanged={this.onViewableItemsChanged}
              viewabilityConfig={{
                itemVisiblePercentThreshold: 95
              }}

                refreshing={this.state.isLoading}
                onRefresh={this.getFollowingPosts}
                renderItem={(posts, id) => {
                  const items = posts.item;
                  return (
                    <View style={styles.card}>                    

                      <View style={styles.cardContent}>
                        {items.file_Name.toString().endsWith("mp4") ?
                          <View>
                            <Video
                              onEnd={this.onEnd}
                              onLoad={this.onLoad}
                              onLoadStart={this.onLoadStart}
                              onProgress={this.onProgress}
                              paused={this.state.paused}
                              ref={videoPlayer => (this.videoPlayer = videoPlayer)}
                              onFullScreen={this.state.isFullScreen}
                              resizeMode="cover"
                              volume={10}
                              source={{ uri: "http://192.168.1.3:3200/" + items.file_Name }}
                              style={{ width: '100%', height: '100%', top: 0, left: 0, bottom: 0, right: 0, }}

                            />
                            <MediaControls
                              duration={this.state.duration}
                              isLoading={this.state.isLoading}
                              mainColor="#333"
                              onFullScreen={this.onFullScreen}
                              onPaused={this.onPaused}
                              onReplay={this.onReplay}
                              onSeek={this.onSeek}
                              onSeeking={this.onSeeking}
                              playerState={this.state.playerState}
                              progress={this.state.currentTime}
                              toolbar={this.renderToolbar()}
                            />
                          </View>
                       </View>
      />
   </View>
  )
 }

If anyone find anything wrong in code code please tells me , If anyone better solution please help me with the code ... Thank you to all programmers ...如果有人在代码中发现任何错误,请告诉我,如果有更好的解决方案,请帮助我编写代码......谢谢所有程序员......

Well for one, const { duration, paused, overlay } = this.state is declared but you're still accessing state in JSX with this.state .一方面, const { duration, paused, overlay } = this.state已声明,但您仍然使用this.state访问 JSX 中的状态。

Your onViewableItemsChanged function doesn't mutate state or do anything besides log stuff out to the console.您的onViewableItemsChanged函数不会改变状态,也不会做任何事情,除了将内容记录到控制台之外。

And you either left out or didn't finish writing the onLoad, onEnd, onProgress, etc. functions.你要么遗漏了要么没有完成 onLoad、onEnd、onProgress 等函数的编写。 So, it's kind of impossible to know exactly what's going on.所以,要确切地知道发生了什么是不可能的。 If you just omitted those functions in order to post this question, are you updating the paused state in any of those functions?如果您只是为了发布这个问题而省略了这些函数,那么您是否更新了这些函数中的任何一个的paused状态?

import { Viewport } from '@skele/components'

Use this component, it resolved for me, this component helps you track one video at a time and plays only the video that is visible on screen使用此组件,它为我解决了,此组件可帮助您一次跟踪一个视频并仅播放屏幕上可见的视频

even, am a newbie in react native, hence this is the easiest solution one could get甚至,我是 React Native 的新手,因此这是最简单的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM