我已经在这段代码中使用了一段时间,试图将我的视频从本地目录列出到我的React Native应用程序中。在我开始项目时,我曾在这方面作为新手问过几个问题,但没有得到任何答案。我以某种方式设法达到了这一点。我已经能够从本地目录中获取视频,但是现在的问题是我的视频仅显示灰色缩略图,而当我点击任何视频时,它会出现ReferenceError:找不到变量:videos
以下是我的屏幕截图和代码。请我纠正在此代码上做的错误的事情。在此先感谢您的帮助。
constructor(Props) {
super(Props);
this.state = {
videos: [],
playing: false,
screenType: 'content',
resizeMode: 'contain',
currentTime: 0,
videoPlayer: null,
duration: 0,
isFullScreen: false,
isLoading: true,
paused: false,
playerState: PLAYER_STATES.PLAYING,
};
这是我的构造函数
componentDidMount() {
var filePath = RNFetchBlob.fs.dirs.MovieDir + '/Voc Vedos/';
RNFetchBlob.fs.ls(filePath)
.then(files => {
this.setState({videos:files});
console.warn(files);
}).catch(err=>alert(err.toString()))
}
这是我从设备上的本地目录获取视频的地方
render() {
const { duration, currentTime, paused, overlay } = this.state
return(
<View style={styles.container}>
<FlatList
data={this.state.videos}
keyExtractor={item=>item}
ItemSeparatorComponent={() => { return (<View style={styles.separator} />) }}
// viewabilityConfig={this.viewabilityConfig}
// onViewableItemsChanged={this.onViewableItemsChanged}
// viewabilityConfig={{
// itemVisiblePercentThreshold: 95
// }}
numColumns={3}
renderItem={({ item, index, separators }) => (
<TouchableOpacity
onPress={() => this._onPress(item)}
style={{width:100,height:100}}>
<View
style={{width:100,height:100, margin:8}}
>
<Video
source ={{uri: '/storage/emulated/0/Movies/Voc Vedos/'+{item}}}
ref={(ref: Video) => { this.video = ref }}
style={{width:100,height:100}}
rate={this.state.rate}
paused={this.state.paused}
volume={this.state.volume}
muted={this.state.muted}
resizeMode={this.state.resizeMode}
onLoad={this.onLoad}
onProgress={this.onProgress}
onEnd={this.onEnd}
onAudioBecomingNoisy={this.onAudioBecomingNoisy}
onAudioFocusChanged={this.onAudioFocusChanged}
/>
<MediaControls
isFullScreen={this.state.isFullScreen}
duration={duration}
isLoading={this.state.isLoading}
mainColor="purple"
onFullScreen={noop}
onPaused={this.state.onPaused}
onReplay={this.state.onReplay}
onSeek={this.state.onSeek}
onSeeking={this.state.onSeeking}
playerState={this.state.playerState}
progress={currentTime}
/>
</View>
</TouchableOpacity>
)}
/>
</View>
这是我的渲染代码。我需要有关如何正确显示我的视频以及在点击时播放视频的帮助
根据react-native-video docs:对于设备存储中的文件源,必须在路径开头写入'file://'
例:
来源= {{uri:'file:///sdcard/Movies/sintel.mp4'}}
阅读文档https://github.com/react-native-community/react-native-video#source
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句