import React, { Component } from "react";
import {
Platform,
Text,
View,
ImageBackground,
Button,
Image,
TouchableOpacity,
StyleSheet
} from "react-native";
import { createStackNavigator, createAppContainer } from "react-navigation";
import Video from "react-native-video";
import SeekBar from "../components/SeekBar";
const instructions = Platform.select({
ios: "Press Cmd+R to reload,\n" + "Cmd+D or shake for dev menu",
android:
"Double tap R on your keyboard to reload,\n" +
"Shake or press menu button for dev menu"
});
type Props = {};
export default class CusVideo extends React.Component {
state = {
rate: 1,
volume: 1,
muted: false,
resizeMode: "stretch",
duration: 0.0,
currentTime: 0.0,
paused: false,
wheel: false,
player_status_icon: require("../images/video/start.png"),
isFull: false,
needback: this.props.needback
};
render() {
return (
);
}
loadStart() {
// alert("loadStart");
}
onBuffer({ isBuffering }: { isBuffering: boolean }) {
//true为正在加载,false为没有加载,此处给loading提示
console.log("isBuffering:" + isBuffering);
}
onLoad = data => {
//获取的是秒数
this.setState({ duration: data.duration });
this.seekbar.setMax(data.duration);
};
onProgress = data => {
this.setState({
currentTime: data.currentTime
});
this.seekbar.setProgress(this.state.currentTime);
};
onError() {
alert("播放器异常");
}
onEnd() {
alert("播放结束");
}
play() {
if (this.state.paused) {
this.start();
} else {
this.pause();
}
}
pause() {
this.setState({
paused: true,
player_status_icon: require("../images/video/start.png")
});
this.player_icon_index = 1;
}
start() {
this.setState({
paused: false,
player_status_icon: require("../images/video/pause.png")
});
this.player_icon_index = 0;
}
showToast(params) {
// ToastExample.message(params);
ToastExample.show(params, ToastExample.SHORT);
}
seekTo(progress) {
this.player.seek(progress);
}
presentFullscreenPlayer() {
if (this.state.isFull) {
this.setState({
isFull: false,
needback: this.props.needback
});
} else {
this.setState({
isFull: true,
needback: true
});
}
this.props.videofullScreenPlayer();
this.seekbar.setProgress(this.state.currentTime);
}
touch_up_callback(progress) {
//抬起之后,获取算出来的progress
this.setState({
currentTime: progress
});
this.seekTo(progress);
}
refreshVideo() {
this.setState({
duration: 0,
currentTime: 0
});
}
videoBackClick() {
// if (this.props.needback != undefined && this.props.needback) {
// }
if (this.state.isFull) {
//全屏状态下,变小屏
this.presentFullscreenPlayer();
} else {
this.props.videoback();
}
}
}
const styles = StyleSheet.create({
player_controller: {
flexDirection: "row",
position: "absolute",
width: "100%",
height: 50,
alignItems: "center",
justifyContent: "center",
bottom: 0
},
player_pause_icon: {
width: "80%",
resizeMode: "center",
height: "40%",
justifyContent: "center",
alignItems: "center",
left: 5
},
fullscreen_icon: {
width: "80%",
resizeMode: "center",
height: "30%",
justifyContent: "center",
alignItems: "center"
},
player_time: {
fontSize: 18,
color: "white",
alignItems: "center",
justifyContent: "center",
textAlignVertical: "center",
textAlign: "center"
}
});
/**
* 将秒转换为 分:秒
* s int 秒数
*/
function formatTime(s) {
//计算分钟
//算法:将秒数除以60,然后下舍入,既得到分钟数
var h;
h = Math.floor(s / 60);
//计算秒
//算法:取得秒%60的余数,既得到秒数
s = Math.round(s % 60);
//将变量转换为字符串
h += "";
s += "";
//如果只有一位数,前面增加一个0
h = h.length == 1 ? "0" + h : h;
s = s.length == 1 ? "0" + s : s;
return h + ":" + s;
}
class VideoBack extends Component {
render() {
if (this.props.needback) {
return (
this.props.videoback()}
>
);
} else {
return null;
}
}
}
/**
使用方法
(this.video = view)}
needback={true} //(是否需要小窗口的返回按钮)
videoback={this.clickVideoBack.bind(this)}//(小窗口返回按钮的事件)
videofullScreenPlayer={this.fullScreenPlayer.bind(this)}//(点击全屏按钮的事件)
style={{
flex: this.state.video_flex,
width: this.state.video_width,
height: this.state.video_height
}}
/>
*/