【发布时间】:2020-01-02 06:28:04
【问题描述】:
您好,我正在使用带有 FlatList 的 React-Native 来制作像 TikTok 这样的视频库应用程序,当我在制作应用程序时,我遇到了视频播放和暂停选项的错误。当我打开应用程序时,所有视频都在后台一次播放,但我想播放唯一一个在屏幕或视点中可见的视频。 所以一旦检查下面的代码并给我任何更好的建议..
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>
)
}
如果有人在代码中发现任何错误,请告诉我,如果有人更好的解决方案,请帮助我编写代码... 感谢所有程序员...
【问题讨论】:
-
嗨!你这样做了吗?我也在使用 FlatList 开发这种类型的功能。如果你这样做了,请告诉我。
标签: javascript android react-native