React Native AF Video Player 是一个基于 React Native 和蘋果 AVFoundation 框架的视频播放器组件。以下是如何使用该组件的示例代码:
import React, { useRef, useState } from 'react';
import { View, StyleSheet, Button } from 'react-native';
import AFVideoPlayer from 'react-native-af-video-player';
const App = () => {
const videoPlayerRef = useRef(null);
const [isPlaying, setIsPlaying] = useState(false);
const playVideo = () => {
if (videoPlayerRef.current) {
videoPlayerRef.current.play();
setIsPlaying(true);
}
};
const pauseVideo = () => {
if (videoPlayerRef.current) {
videoPlayerRef.current.pause();
setIsPlaying(false);
}
};
return (
<View style={styles.container}>
<AFVideoPlayer
ref={videoPlayerRef}
style={styles.videoPlayer}
src="http://www.example.com/path/to/your/video.mp4"
/>
<View style={styles.buttonContainer}>
<Button onPress={playVideo} title={isPlaying ? '正在播放' : '播放视频'} />
<Button onPress={pauseVideo} title="暂停视频" />
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
videoPlayer: {
height: 250,
width: 300,
},
buttonContainer: {
marginTop: 10,
},
});
export default App;
这段代码展示了如何在 React Native 应用中集成并使用 AFVideoPlayer 组件。它包括播放和暂停视频的功能,并通过按钮触发。此外,它还展示了如何使用 useRef
和 useState
钩子来管理组件状态。