Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react native videos in carousel

I have been trying to play single video from a slider containing of multiple videos and images.. What i used and tried is in below. 1. react-native-video, 2. react-native-snap-carousel

How to pause and play the videos wrapped in horizontal carousel and also which are in vertical FlatList Feeds

This is my carousel :

<View style={styles.sliderImgView}>
     <Carousel
       ref={(c) => { this._carousel = c; }}
       data={chordData.images}
       firstItem={0}
       autoplay={autoPlay}
       layout={layout}
       loop={loop}
       renderItem={this._renderItem}
       onSnapToItem={(ind) => this.setState({ activeSlide: ind })}
       loopClonesPerSide={bannersDataLength}
       sliderWidth={SCREEN_WIDTH}
       itemWidth={SCREEN_WIDTH} />
</View>

And my _renderItem is here :

if (item.mediaType === "image") {
return (
    <View style={[styles.sliderImgView, GlobalStyles.ajCenter]} key={index}>
        <Image source={{ uri: item.imgUrl }} resizeMode={"cover"} style={[styles.sliderImageStyle]} />
    </View>
)
} else {
    return (
        <View style={[styles.sliderImgView, GlobalStyles.ajCenter]} key={index}>
            <Video
                source={{ uri: item.imgUrl }}  // Can be a URL or a local file.
                ref={(ref) => { this.player = ref }}  // Store reference
                resizeMode={"cover"}
                paused={index !== this.state.activeSlide}
                onLoad={this.onVideoLoad}
                onError={this.onVideoError}
                controls={false}
                style={styles.backgroundVideo} />
        </View>
    )
}

And my Array look's like this :

result: [
{
    id: 1,
    title: "Feed Title",
    description: "Feed description....",
    data: [
        {
            id: 1,
            mediaType: "image",
            imgUrl: "https://images.unsplash.com/photo-1473177027534-53d906e9abcf?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1049&q=80"
        },
        {
            id: 2,
            mediaType: "video",
            imgUrl: "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
        },
        {
            id: 3,
            mediaType: "image",
            imgUrl: "https://images.unsplash.com/photo-1473177027534-53d906e9abcf?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1049&q=80"
        },
        {
            id: 4,
            mediaType: "video",
            imgUrl: "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
        },
    ]
},
{
    id: 2,
    title: "Feed Title",
    description: "Feed description....",
    data: [
        {
            id: 1,
            mediaType: "image",
            imgUrl: "https://images.unsplash.com/photo-1587269012604-b20cfbca7b16?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=849&q=80"
        }
    ]
},
{
    id: 3,
    title: "Feed Title",
    description: "Feed description....",
    data: [
        {
            id: 1,
            mediaType: "image",
            imgUrl: "https://images.unsplash.com/photo-1473177027534-53d906e9abcf?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1049&q=80"
        },
        {
            id: 2,
            mediaType: "video",
            imgUrl: "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
        },

    ]
},
{
    id: 4,
    title: "Feed Title",
    description: "Feed description....",
    data: [
        {
            id: 1,
            mediaType: "image",
            imgUrl: "https://images.unsplash.com/photo-1584679109597-c656b19974c9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=80"
        }
    ]
}
]

I don't want to show the player control that's why i hide it.. just like Instagram. I don't know whether i should use this or not.

Present issue is: i should only play the video which is in visible section of user eyes which is in FlatList (i had't mention my flatList code as it is just list_design). i had scroll list with multiple objects with media data in arrays. how can i manage to stop playing id number 1 object of data array with id 2 or 4 active video index when other id number 3's media is in active.

enter image description here

I just want to achieve same as like Instagram post with no performance lagging issues. can anyone please suggest or help me to achieve this.

like image 434
Lucifer_Latif Avatar asked Jun 19 '20 11:06

Lucifer_Latif


People also ask

How do you use carousel in React Native?

To create a carousel using React Native, you would have to do the following: Use React Native's FlatList API to display our items and configure it to make the list horizontal. Later on, write more code to display custom animations when the user swipes through the slider. Furthermore, build pagination functionality.

How do you use React Native snap carousel in functional component?

Create a new react-native project from your terminal and enter into that project. After doing that, install the carousel package react-native-snap-carousel from your terminal. Next, go to your App. js and refactor your code to include the react-native-snap-carousel package.


1 Answers

Below did the magic controlling other feed's post media sliders to pause the video.

Add the below two props to your FlatList to get the current visible Index., and when ever the current visible index value changes update in componentDidUpdate and call your videoLoad method.

viewabilityConfig={{viewAreaCoveragePercentThreshold: 200}}
onViewableItemsChanged={this.onViewableItemsChanged}

onViewableItemsChanged = ({ viewableItems, changed }) => {
   if (viewableItems && viewableItems.length > 0) {
        this.setState({ currentVisibleIndex: viewableItems[0].index });
   }
}

componentDidUpdate(prevProps, prevState){
   if (prevProps.currentVisibleIndex !== this.props.currentVisibleIndex) {
         this.setState({ currentVisibleIndex: this.props.currentVisibleIndex }, () => {
            this.onVideoLoad();
         })
         //console.log("Check prevProps: " + prevProps.currentVisibleIndex + "----> Check pevState: " + prevState.currentVisibleIndex);
        }
    }
like image 137
Lucifer_Latif Avatar answered Oct 17 '22 14:10

Lucifer_Latif