VideoExplain.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. import courseDetails from '../services/courseDetails'
  26. export default class VideoExplain extends Component {
  27. state = {
  28. isFull: false,
  29. video_height: 210,
  30. statusbar_hidden: false,
  31. videoStyle: {
  32. width: "100%"
  33. },
  34. collectImage: require('../images/courseDetails/collect.png'),
  35. isCollect: true,
  36. numberLine: 2
  37. };
  38. componentWillReceiveProps(nextProps) {
  39. console.log(nextProps.dataList.id)
  40. const targetCode = nextProps.dataList.id;
  41. courseDetails.getisFavorites({
  42. targetCode,
  43. }).then(res => {
  44. console.log('--------', res)
  45. if (res.code == 200 && res.data) {
  46. this.setState({
  47. collectImage: require('../images/courseDetails/have_collect.png'),
  48. isCollect: false
  49. })
  50. }else {
  51. this.setState({
  52. collectImage: require('../images/courseDetails/collect.png'),
  53. isCollect: true
  54. })
  55. }
  56. }).catch(error => console.log(error))
  57. }
  58. render() {
  59. return (
  60. <View style={this.state.videoStyle}>
  61. <StatusBar
  62. // backgroundColor={"transparent"}
  63. barStyle={"dark-content"}
  64. backgroundColor={"white"}
  65. translucent={false}
  66. hidden={this.state.statusbar_hidden}
  67. />
  68. {/* <View style={{ width: "100%", height: 210 }} /> */}
  69. <CusVideo
  70. show={true} //是否显示
  71. uri={this.props.uri} //播放路径
  72. ref={view => (this.video = view)} //设置ID
  73. needback={true}
  74. videoback={this.back.bind(this)}
  75. videofullScreenPlayer={this.fullScreenPlayer.bind(this)}
  76. onError={this.onError.bind(this)}
  77. onEnd={this.onEnd.bind(this)}
  78. style={{
  79. width: "100%",
  80. height: this.state.video_height
  81. }}
  82. />
  83. <View style={styles.videoExplain}>
  84. <View style={styles.title}>
  85. <Text style={styles.font}>{this.props.dataList.title}</Text>
  86. <View style={styles.icon}>
  87. <TouchableOpacity onPress={this.collection.bind(this)}>
  88. <Image source={this.state.collectImage} style={styles.iconSize} />
  89. </TouchableOpacity>
  90. <TouchableOpacity onPress={this.share}>
  91. <Image source={require('../images/courseDetails/share.png')} style={styles.iconSize} />
  92. </TouchableOpacity>
  93. </View>
  94. </View>
  95. <View style={[styles.title, styles.tops]}>
  96. <Text style={styles.font}>课程介绍:</Text>
  97. <Text style={styles.clor} onPress={() => this.open()}>更多 ></Text>
  98. </View>
  99. <Text style={[ styles.clor, styles.introduce]} numberOfLines={this.state.numberLine}>{this.props.dataList.description}</Text>
  100. </View>
  101. </View>
  102. );
  103. }
  104. // 全屏播放
  105. fullScreenPlayer() {
  106. if (!this.state.isFull) {
  107. Orientation.lockToLandscape();
  108. this.setState({
  109. statusbar_hidden: true,
  110. isFull: true,
  111. video_height: "100%",
  112. videoStyle: {
  113. width: "100%",
  114. height: width
  115. }
  116. });
  117. } else {
  118. Orientation.lockToPortrait();
  119. this.setState({
  120. statusbar_hidden: false,
  121. isFull: false,
  122. video_height: 210,
  123. videoStyle: {
  124. width: "100%"
  125. }
  126. });
  127. }
  128. this.props.full(this.state.isFull);
  129. }
  130. // 播放器异常
  131. onError() {
  132. alert("播放器异常");
  133. }
  134. // 播放结束
  135. onEnd() {
  136. alert("播放结束");
  137. this.video.refreshVideo();
  138. }
  139. back(){
  140. this.props.videoback();
  141. }
  142. start(){
  143. this.video.start();
  144. }
  145. componentWillMount() {
  146. this.backlistener=BackHandler.addEventListener(
  147. "hardwareBackPress",
  148. this.onBackAndroid
  149. );
  150. }
  151. componentWillUnmount() {
  152. console.log("componentWillUnmount")
  153. if( this.backlistener){
  154. BackHandler.removeEventListener(
  155. "hardwareBackPress",
  156. this.onBackAndroid
  157. );
  158. }
  159. }
  160. // 播放器返回
  161. onBackAndroid=() => {
  162. if (this.state.isFull) {
  163. return true;
  164. } else {
  165. alert("返回。。。");
  166. }
  167. }
  168. // 收藏
  169. collection() {
  170. if (this.state.isCollect) {
  171. this.setState({
  172. collectImage: require('../images/courseDetails/have_collect.png'),
  173. isCollect: false
  174. })
  175. }else {
  176. this.setState({
  177. collectImage: require('../images/courseDetails/collect.png'),
  178. isCollect: true
  179. })
  180. }
  181. const targetCode = this.props.dataList.id;
  182. const templateCode = this.props.dataList.templateCode;
  183. const title = this.props.dataList.title;
  184. courseDetails.setFavorites({
  185. targetCode,
  186. templateCode,
  187. title,
  188. "iconImg": ""
  189. }).then(res => {
  190. console.log(res)
  191. }).catch(error => console.log(error))
  192. }
  193. // 分享
  194. share() {
  195. alert('点击分享')
  196. }
  197. // 点击更多展开说明
  198. open() {
  199. this.setState({
  200. numberLine: 10
  201. })
  202. }
  203. }
  204. const styles = StyleSheet.create({
  205. videoExplain: {
  206. width: "100%",
  207. backgroundColor: "white",
  208. paddingLeft: 20,
  209. paddingRight: 20,
  210. paddingTop: 13,
  211. paddingBottom: 13
  212. },
  213. title: {
  214. width: "100%",
  215. display: "flex",
  216. flexDirection: "row",
  217. justifyContent: "space-between",
  218. alignItems: "center"
  219. },
  220. font: {
  221. fontSize: 18,
  222. color: "black",
  223. fontWeight: "bold"
  224. },
  225. clor: {
  226. fontSize: 14,
  227. color: "black"
  228. },
  229. icon: {
  230. display: "flex",
  231. flexDirection: "row"
  232. },
  233. iconSize: {
  234. width: 20,
  235. height: 20,
  236. marginLeft: 10
  237. },
  238. tops: {
  239. marginTop: 18
  240. },
  241. introduce: {
  242. marginTop: 5
  243. }
  244. });