VideoExplain.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. *
  5. * @format
  6. * @flow
  7. */
  8. import React, { Component } from "react";
  9. import {
  10. StyleSheet,
  11. Text,
  12. View,
  13. Image,
  14. ScrollView,
  15. StatusBar,
  16. BackHandler,
  17. FlatList,
  18. TouchableOpacity
  19. } from "react-native";
  20. import Orientation from "react-native-orientation";
  21. import CusVideo from "./CusVideo";
  22. // 获取屏幕宽高
  23. import { Dimensions } from 'react-native'
  24. const { height, width } = Dimensions.get('window');
  25. export default class VideoExplain extends Component {
  26. state = {
  27. isFull: false,
  28. video_height: 210,
  29. statusbar_hidden: false,
  30. videoStyle: {
  31. width: "100%"
  32. },
  33. collectImage: require('../images/courseDetails/collect.png'),
  34. isCollect: true,
  35. numberLine: 2
  36. };
  37. render() {
  38. return (
  39. <View style={this.state.videoStyle}>
  40. <StatusBar
  41. // backgroundColor={"transparent"}
  42. barStyle={"dark-content"}
  43. backgroundColor={"white"}
  44. translucent={false}
  45. hidden={this.state.statusbar_hidden}
  46. />
  47. {/* <View style={{ width: "100%", height: 210 }} /> */}
  48. <CusVideo
  49. show={true} //是否显示
  50. uri={this.props.uri} //播放路径
  51. ref={view => (this.video = view)} //设置ID
  52. needback={true}
  53. videoback={this.back.bind(this)}
  54. videofullScreenPlayer={this.fullScreenPlayer.bind(this)}
  55. onError={this.onError.bind(this)}
  56. onEnd={this.onEnd.bind(this)}
  57. style={{
  58. width: "100%",
  59. height: this.state.video_height
  60. }}
  61. />
  62. <View style={styles.videoExplain}>
  63. <View style={styles.title}>
  64. <Text style={styles.font}>{this.props.dataList.title}</Text>
  65. <View style={styles.icon}>
  66. <TouchableOpacity onPress={this.collection.bind(this)}>
  67. <Image source={this.state.collectImage} style={styles.iconSize} />
  68. </TouchableOpacity>
  69. <TouchableOpacity onPress={this.share}>
  70. <Image source={require('../images/courseDetails/share.png')} style={styles.iconSize} />
  71. </TouchableOpacity>
  72. </View>
  73. </View>
  74. <View style={[styles.title, styles.tops]}>
  75. <Text style={styles.font}>课程介绍:</Text>
  76. <Text style={styles.clor} onPress={() => this.open()}>更多 ></Text>
  77. </View>
  78. <Text style={[ styles.clor, styles.introduce]} numberOfLines={this.state.numberLine}>{this.props.dataList.description}</Text>
  79. </View>
  80. </View>
  81. );
  82. }
  83. // 全屏播放
  84. fullScreenPlayer() {
  85. if (!this.state.isFull) {
  86. Orientation.lockToLandscape();
  87. this.setState({
  88. statusbar_hidden: true,
  89. isFull: true,
  90. video_height: "100%",
  91. videoStyle: {
  92. width: "100%",
  93. height: width
  94. }
  95. });
  96. } else {
  97. Orientation.lockToPortrait();
  98. this.setState({
  99. statusbar_hidden: false,
  100. isFull: false,
  101. video_height: 210,
  102. videoStyle: {
  103. width: "100%"
  104. }
  105. });
  106. }
  107. this.props.full(this.state.isFull);
  108. }
  109. // 播放器异常
  110. onError() {
  111. alert("播放器异常");
  112. }
  113. // 播放结束
  114. onEnd() {
  115. alert("播放结束");
  116. this.video.refreshVideo();
  117. }
  118. back(){
  119. this.props.videoback();
  120. }
  121. start(){
  122. this.video.start();
  123. }
  124. componentWillMount() {
  125. this.backlistener=BackHandler.addEventListener(
  126. "hardwareBackPress",
  127. this.onBackAndroid
  128. );
  129. }
  130. componentWillUnmount() {
  131. console.log("componentWillUnmount")
  132. if( this.backlistener){
  133. BackHandler.removeEventListener(
  134. "hardwareBackPress",
  135. this.onBackAndroid
  136. );
  137. }
  138. }
  139. // 播放器返回
  140. onBackAndroid=() => {
  141. if (this.state.isFull) {
  142. return true;
  143. } else {
  144. alert("返回。。。");
  145. }
  146. }
  147. // 收藏
  148. collection() {
  149. if (this.state.isCollect) {
  150. this.setState({
  151. collectImage: require('../images/courseDetails/have_collect.png'),
  152. isCollect: false
  153. })
  154. }else {
  155. this.setState({
  156. collectImage: require('../images/courseDetails/collect.png'),
  157. isCollect: true
  158. })
  159. }
  160. }
  161. // 分享
  162. share() {
  163. alert('点击分享')
  164. }
  165. // 点击更多展开说明
  166. open() {
  167. this.setState({
  168. numberLine: 10
  169. })
  170. }
  171. }
  172. const styles = StyleSheet.create({
  173. videoExplain: {
  174. width: "100%",
  175. backgroundColor: "white",
  176. paddingLeft: 20,
  177. paddingRight: 20,
  178. paddingTop: 13,
  179. paddingBottom: 13
  180. },
  181. title: {
  182. width: "100%",
  183. display: "flex",
  184. flexDirection: "row",
  185. justifyContent: "space-between",
  186. alignItems: "center"
  187. },
  188. font: {
  189. fontSize: 18,
  190. color: "black",
  191. fontWeight: "bold"
  192. },
  193. clor: {
  194. fontSize: 14,
  195. color: "black"
  196. },
  197. icon: {
  198. display: "flex",
  199. flexDirection: "row"
  200. },
  201. iconSize: {
  202. width: 20,
  203. height: 20,
  204. marginLeft: 10
  205. },
  206. tops: {
  207. marginTop: 18
  208. },
  209. introduce: {
  210. marginTop: 5
  211. }
  212. });