react native随手记之视频播放器video

react native 随手记之视频播放器Video


一、导入三方库

1.video

yarn add react-native-video

2.slider

yarn add react-native-slider

二、 代码示例

import React, {Component} from 'react';
import {StyleSheet, View, Image, TouchableOpacity} from 'react-native';
import Video from 'react-native-video';
import Slider from 'react-native-slider';

export default class TrainDetailVideo extends Component {
    constructor(props) {
        super(props);
        this.state = {
            video: Video,
            pauseShow: true,
            pause: true,
            progress: 0,
            maxProgress: 1,
        };
    }

    render() {
        const {videoUrl, videoPicUrl} = this.props;
        return (
            <View style={styles.container}>
                <Video
                    ref={(ref: Video) => {
                        this.video = ref;
                    }}
                    paused={this.state.pause}
                    style={styles.videoStyle}
                    source={{uri: videoUrl}}
                    resizeMode={'contain'}
                    // repeat={true}
                    onLoad={(e) => {
                        this.setState({maxProgress: e.duration});
                    }}
                    onProgress={(e) => {
                        this.setState({
                            progress: e.currentTime,
                        });
                    }}
                    poster={videoPicUrl}
                    posterResizeMode={'cover'}
                    onEnd={() => {
                        this.setState({
                            pause: true,
                            progress: this.state.maxProgress,
                        });
                    }}
                />
                <View style={styles.viewControll}>
                    {this.state.pauseShow === true ?
                        <View style={styles.viewPause}>
                            <TouchableOpacity
                                onPress={() => {

                                    this.setState({
                                        pause: false,
                                        pauseShow: false,
                                    });
                                }}>
                                <Image style={styles.imgPauseBig}
                                       source={require('../../../../assets/ic_video_pause.png')}/>
                            </TouchableOpacity>

                        </View>
                        :
                        <View style={styles.viewBottomControll}>
                            <View style={{flexDirection: 'row', alignItems: 'center', marginBottom: 5}}>
                                <TouchableOpacity
                                    onPress={() => {
                                        if (this.state.progress >= this.state.maxProgress) {
                                            this.video.seek(0);
                                        }
                                        this.setState({
                                            pause: this.state.pause === true ? false : true,
                                            progress: this.state.progress >= this.state.maxProgress ? 0 : this.state.progress,
                                        });
                                    }}>  
                                    <Image style={styles.imgPauseSmall}
                                           source={this.state.pause === true ? require('../../../../assets/ic_small_pause.png') : require('../../../../assets/ic_small_play.png')}/>
                                </TouchableOpacity>

                                <Slider style={styles.slider}
                                        value={this.state.progress}
                                        maximumValue={this.state.maxProgress}
                                        minimumTrackTintColor={'white'}
                                        maximumTrackTintColor={'#636D76'}
                                        thumbTintColor={'white'}
                                        thumbStyle={{width: 10, height: 10}}
                                    // disabled={true}
                                        onValueChange={(value) => {
                                            this.video.seek(value);
                                            this.setState({
                                                progress: value,
                                                pause: value >= this.state.maxProgress ? true : false,
                                            });
                                        }}/>
                            </View>
                        </View>}


                </View>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        position: 'relative',
        marginLeft: 16,
        marginRight: 16,
        height: 196,
        marginBottom: 10,
        backgroundColor: 'black',
        justifyContent: 'center',
        alignItems: 'center',
    },
    videoStyle: {
        flex: 1,
        width: '100%',
        height: 196,
    },
    viewControll: {
        position: 'absolute',
        width: '100%',
        height: '100%',

    },
    viewPause: {
        width: '100%',
        height: '100%',
        justifyContent: 'center',
        alignItems: 'center',
    },
    viewBottomControll: {
        width: '100%',
        height: '100%',
        flexDirection: 'row',
        alignItems: 'flex-end',
        paddingRight: 5,
        paddingLeft: 5,

    },
    imgPauseBig: {
        height: 50,
        width: 50,
    },
    imgPauseSmall: {
        height: 30,
        width: 30,
    },
    slider: {
        marginLeft: 10,
        marginRight: 10,
        height: 5,
        flex: 1,
    },
});



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值