我一直试图从包含多个视频和图像的滑块播放单个视频。 我在下面使用和尝试过的。 1。 react-native-video,2。react-native-snap-carousel
如何暂停和播放水平轮播以及垂直FlatList Feed中的视频
这是我的旋转木马:
<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>
我的_renderItem在这里:
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>
)
}
我的数组看起来像这样:
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"
}
]
}
]
我不想显示播放器控件,这就是为什么我将其隐藏..就像Instagram。 我不知道我是否应该使用this。
目前的问题是:我只应该播放FlatList中用户眼睛可见部分中的视频(我没有提到我的flatList代码,因为它只是list_design)。我有多个对象的滚动列表,数组中有媒体数据。当其他ID号3的媒体处于活动状态时,我如何设法停止播放ID为2或4的视频数组的ID号为1的对象。 / p>
我只想实现与Instagram帖子一样的效果,而不会出现性能滞后的问题。 谁能建议或帮助我实现这一目标。
答案 0 :(得分:2)
下面魔术控制其他供稿的发布媒体滑块以暂停视频。
将以下两个道具添加到FlatList中以获取当前可见索引。当当前可见索引值更改时,都会在componentDidUpdate中更新并调用您的videoLoad方法。
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);
}
}
答案 1 :(得分:0)
实现这一目标的最佳方法是使用 inviewport