react-native-video的簡單使用
【react-native-video的簡單使用】安裝與依賴請參考github地址
https://github.com/react-native-community/react-native-video
//---------
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Platform,
ActivityIndicator,
TouchableOpacity,
ScrollView,
Image,
ListView,
} from 'react-native';
import Video from 'react-native-video';
import Dimensions from 'Dimensions';
import Icon from 'react-native-vector-icons/Ionicons';
import config from '../common/config';
import request from '../common/request';
const {width,height} = Dimensions.get('window');
export default class Detail extends Component {
constructor(props){
super(props);
this.state={
rowData:this.props.rowData,
rate: 10,//播放速率
volume: 1,
muted: true,//静音
resizeMode: 'contain',
paused: false,//暂停
videoError:false,//错误duration: 0.0,
currentTime: 0.0,
videoLoaded:false,
playing:false,
playEnd:false,dataSource:new ListView.DataSource({
rowHasChanged:(r1,r2) => r1 !== r2,
})
}// this._onLoadStart = this._onLoadStart.bind(this);
// this._onLoad = this._onLoad.bind(this);
this._onProgress = this._onProgress.bind(this);
// this._onEnd = this._onEnd.bind(this);
// this._onError = this._onError.bind(this);
this._rePlay = this._rePlay.bind(this);
this._resume = this._resume.bind(this);
this._pause = this._pause.bind(this);
this._pop = this._pop.bind(this);
this._renderRow = this._renderRow.bind(this);
}_renderRow(rowData_reply){
return(
{rowData_reply.replyBy.nickname}
{rowData_reply.content}
)
}componentDidMount(){
this._fetchData();
//从服务器获取数据
}
_fetchData(){let url = config.api.base + config.api.comments;
//发送网络请求
request.get(url,{
id:"112233",
accessToken:'jjjj'
}).then(
(data)=>{
if(data && data.success){
let comments = data.data;
if(comments && comments.length > 0){
this.setState({
dataSource:this.state.dataSource.cloneWithRows(comments)
})
}
}
}
).catch(
(error)=>{
console.log('数据有问题');
}
)
}_pop(){
let {navigator} = this.props;
if(navigator){
navigator.pop();
}
}
_resume(){
if(this.state.paused){
this.setState({
paused:false
});
}
}
_pause(){
if(!this.state.paused){
this.setState({
paused:true
});
}
}_rePlay(){
this.setState({
playEnd:false,
paused: false,
})
this.refs.videoPlayer.seek(0);
}_onLoadStart=()=>{
console.log('_onLoadStart');
}
_onLoad=(data)=>{
console.log('_onLoad--视频总长度'+data.duration);
this.setState({duration:data.duration});
}
_onProgress(data){
if(!this.state.videoLoaded){
this.setState({
videoLoaded:true,
});
}
if(!this.state.playing && !this.state.playEnd){
console.log('进来改变状态');
this.setState({
playEnd:false,
playing:true,
});
}this.setState({currentTime:data.currentTime});
//console.log('_onProgress---当前时间'+data.currentTime);
}_onEnd=()=>{
this.setState({
playing:false,
paused:true,
playEnd:true,
})
console.log('结束了');
}
_onError=(error)=>{
console.log('错误'+JSON.stringify(error))
this.setState({
videoError:true,
})
}getCurrentTimePercentage() {
if (this.state.currentTime > 0) {
return parseFloat(this.state.currentTime) / parseFloat(this.state.duration);
} else {
return 0;
}
}render() {
let rowData = https://www.it610.com/article/this.state.rowData;
//一个是当前时间,一个是剩余时间
const flexCompleted = this.getCurrentTimePercentage() * 100;
const flexRemaining = (1 - this.getCurrentTimePercentage()) * 100;
return (
{/*导航栏*/}
{/*返回按钮*/}
返回
{/*中间的文字*/}
视频详情页面
{/*播放器*/}
{/*底部内容*/}{/*视频信息*/}
{/*头像*/}
作者:{rowData.author.nickname}
标题:{rowData.title}
{/*评论信息*/}
);
}_backToList = ()=>{
let {navigator} = this.props;
if(navigator){
navigator.pop();
}
}
}let videoBoxHeight = 250;
let videoHeight = videoBoxHeight - 10;
let loadingTop = videoBoxHeight * 0.5 - 30;
let playWH = 60;
let playTop = videoBoxHeight * 0.5 - playWH*0.5;
let failTextTop = videoBoxHeight * 0.5 + playWH*0.5;
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
marginTop:Platform.OS === 'ios'?20:0,
},
videoBox:{
width:width,
height:videoBoxHeight,
backgroundColor:'black'
},
video:{
width:width,
height:videoHeight,
backgroundColor:'black'
},
loading:{
position:'absolute',
top:loadingTop,
width:width,
left:0,
alignItems:'center',
},
progress: {
flex: 1,
flexDirection: 'row',
borderRadius: 3,
overflow: 'hidden',
},
innerProgressCompleted: {
height: 10,
backgroundColor: '#cccccc',
},
innerProgressRemaining: {
height: 10,
backgroundColor: '#2C2C2C',
},
play:{
position:'absolute',
top:playTop,
left:width * 0.5 - playWH*0.5,
width:playWH,
height:playWH,
paddingTop:10,
paddingLeft:22,
backgroundColor:'transparent',
borderColor:'black',
borderWidth:1,
borderRadius:playWH * 0.5,
},
pauseStyle:{
position:'absolute',
top:0,
left:0,
width:width,
height:videoHeight,
},
failText:{
position:'absolute',
left:0,
width:width,
top:failTextTop,
backgroundColor:'transparent',
textAlign:'center',
color:'red',
fontSize:20,
},
navStyle:{
flexDirection:'row',
justifyContent:'center',
alignItems:'center',
width:width,
height:64,
backgroundColor:'#dddddd',
borderBottomWidth:0.5,
borderBottomColor:'black',
},
backBox:{
position:'absolute',
left:12,
top:25,
width:60,
flexDirection:'row',
alignItems:'center'
},
backIcon:{
fontSize:22,
marginRight:5
},
backText:{
fontSize:16
},
navText:{
textAlign:'center',
lineHeight:64,
height:64,
fontSize:18,
width:width - 120,
},
infoBox:{
flexDirection:'row',
width:width,
justifyContent:'center',
marginTop:10,
},
avatar:{
width:60,
height:60,
borderRadius:30,
marginRight:10,
marginLeft:10
},
descBox:{
flex:1,
},
nickname:{
fontSize:18,
},
title:{
marginTop:8,
fontSize:16,
color:'#666',
},
replyBox:{
flexDirection:'row',
width:width,
justifyContent:'center',
marginTop:10,
},
replyAvatar:{
width:40,
height:40,
borderRadius:20,
marginRight:10,
marginLeft:10
},
reply:{
flex:1
},
replyNickname:{
fontSize:16,
},
replyContent:{
marginTop:5,
paddingRight:10}});
效果圖如下:
文章图片
图片.png
推荐阅读
- 热闹中的孤独
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 放屁有这三个特征的,请注意啦!这说明你的身体毒素太多
- 一个人的旅行,三亚
- 布丽吉特,人生绝对的赢家
- 慢慢的美丽
- 尽力
- 一个小故事,我的思考。
- 家乡的那条小河
- 《真与假的困惑》???|《真与假的困惑》??? ——致良知是一种伟大的力量