CusVideo.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. import React, { Component } from "react";
  2. import {
  3. Platform,
  4. Text,
  5. View,
  6. ImageBackground,
  7. Button,
  8. Image,
  9. TouchableOpacity,
  10. PanResponder,
  11. StyleSheet
  12. } from "react-native";
  13. import { createStackNavigator, createAppContainer } from "react-navigation";
  14. import Video from "react-native-video";
  15. import SeekBar from "../components/SeekBar";
  16. const instructions = Platform.select({
  17. ios: "Press Cmd+R to reload,\n" + "Cmd+D or shake for dev menu",
  18. android:
  19. "Double tap R on your keyboard to reload,\n" +
  20. "Shake or press menu button for dev menu"
  21. });
  22. type Props = {};
  23. export default class CusVideo extends React.Component {
  24. constructor(props) {
  25. super(props);
  26. this.pressStatus = false;
  27. this.progress = 0;
  28. this.time_hideController;
  29. }
  30. config = {
  31. changeX: 0,
  32. changeY: 0,
  33. xDiff: 0,
  34. yDiff: 0
  35. };
  36. state = {
  37. player_status_icon: require("../images/video/start.png"),
  38. rate: 1,
  39. volume: 1,
  40. muted: false,
  41. resizeMode: "stretch",
  42. duration: 0.0,
  43. currentTime: 0.0,
  44. paused: true,
  45. wheel: false,
  46. isFull: false,
  47. needback: this.props.needback,
  48. show_controller: true,
  49. show_loading: true,
  50. loading_arr: [
  51. require("../images/video/loading1.png"),
  52. require("../images/video/loading2.png"),
  53. require("../images/video/loading3.png"),
  54. require("../images/video/loading4.png"),
  55. require("../images/video/loading5.png"),
  56. require("../images/video/loading6.png"),
  57. require("../images/video/loading7.png"),
  58. require("../images/video/loading8.png"),
  59. require("../images/video/loading9.png"),
  60. require("../images/video/loading10.png"),
  61. require("../images/video/loading11.png"),
  62. require("../images/video/loading12.png"),
  63. require("../images/video/loading13.png"),
  64. require("../images/video/loading14.png"),
  65. require("../images/video/loading15.png"),
  66. require("../images/video/loading16.png"),
  67. require("../images/video/loading17.png"),
  68. require("../images/video/loading18.png"),
  69. require("../images/video/loading19.png"),
  70. require("../images/video/loading20.png")
  71. ]
  72. };
  73. render() {
  74. if (!this.props.show) {
  75. return null;
  76. }
  77. return (
  78. <View style={[this.props.style, { overflow: "hidden" }]}>
  79. <Video
  80. {...this.videotouch.panHandlers}
  81. style={{ flex: 1 }}
  82. source={{
  83. uri: this.props.uri
  84. }}
  85. ref={ref => {
  86. this.player = ref;
  87. }}
  88. // poster={this.props.poster}
  89. resizeMode={[this.state.resizeMode]}
  90. // posterResizeMode={this.state.resizeMode}
  91. onBuffer={this.onBuffer.bind(this)}
  92. rate={this.state.rate} //播放速率
  93. paused={this.state.paused} //暂停
  94. volume={this.state.volume} //调节音量
  95. muted={this.state.muted} //控制音频是否静音
  96. resizeMode={this.state.resizeMode} //缩放模式
  97. onLoadStart={this.loadStart} // 当视频开始加载时的回调函数
  98. onLoad={this.onLoad} //加载媒体并准备播放时调用的回调函数。
  99. onProgress={this.onProgress} //视频播放过程中每个间隔进度单位调用的回调函数
  100. onAudioBecomingNoisy={this.onAudioBecomingNoisy} //音频变得嘈杂时的回调 - 应暂停视频
  101. onAudioFocusChanged={this.onAudioFocusChanged} //音频焦点丢失时的回调 - 如果焦点丢失则暂停
  102. repeat={this.state.wheel} //确定在到达结尾时是否重复播放视频。
  103. onError={this.onError.bind(this)} // 当视频不能加载,或出错后的回调函数
  104. onEnd={this.onEnd.bind(this)} //视频播放结束时的回调函数
  105. playInBackground={false} // 当app转到后台运行的时候,播放是否暂停
  106. playWhenInactive={true} // [iOS] Video continues to play when control or notification center are shown. 仅适用于IOS
  107. />
  108. <View
  109. style={{
  110. position: "absolute",
  111. width: "100%",
  112. height: "100%",
  113. alignItems: "center",
  114. justifyContent: "center"
  115. }}
  116. >
  117. <Loading
  118. loading_arr={this.state.loading_arr}
  119. show={this.state.show_loading}
  120. />
  121. </View>
  122. <TopController
  123. ref={view => (this.topcontroller = view)}
  124. needback={this.state.needback}
  125. videoback={this.videoBackClick.bind(this)}
  126. />
  127. <BottomController
  128. ref={view => (this.bottomcontroller = view)}
  129. show={this.state.show_controller}
  130. touch_up_callback={this.touch_up_callback.bind(this)}
  131. full_click={this.presentFullscreenPlayer.bind(this)}
  132. play_click={this.play.bind(this)}
  133. duration={this.state.duration}
  134. currentTime={this.state.currentTime}
  135. player_status_icon={this.state.player_status_icon}
  136. />
  137. </View>
  138. );
  139. }
  140. componentWillMount() {
  141. if (this.state.pause) {
  142. }
  143. this.videotouch = PanResponder.create({
  144. onStartShouldSetPanResponder: (evt, gestureState) => true,
  145. onStartShouldSetPanResponderCapture: (evt, gestureState) => true,
  146. onMoveShouldSetPanResponder: (evt, gestureState) => true,
  147. onMoveShouldSetPanResponderCapture: (evt, gestureState) => true,
  148. onPanResponderGrant: (evt, gestureState) => {
  149. this.pressStatus = true;
  150. this.config.changeY = evt.nativeEvent.pageY;
  151. this.config.changeX = evt.nativeEvent.pageX;
  152. if (this.state.show_controller) {
  153. this.hideController();
  154. } else {
  155. this.showController();
  156. }
  157. },
  158. onPanResponderStart: (evt, gestureState) => {
  159. this.pressStatus = true;
  160. },
  161. onPanResponderMove: (evt, gestureState) => {
  162. this.config.yDiff = evt.nativeEvent.pageY - this.config.changeY;
  163. this.config.xDiff = evt.nativeEvent.pageX - this.config.changeX;
  164. this.config.changeY = evt.nativeEvent.pageY;
  165. this.config.changeX = evt.nativeEvent.pageX;
  166. },
  167. onPanResponderEnd: (evt, gestureState) => {
  168. this.pressStatus = true;
  169. },
  170. onPanResponderTerminationRequest: (evt, gestureState) => true,
  171. onPanResponderRelease: (evt, gestureState) => {
  172. if (this.pressStatus) {
  173. this.props.onPress && this.props.onPress();
  174. }
  175. },
  176. onPanResponderTerminate: (evt, gestureState) => {
  177. // 另一个组件已经成为了新的响应者,所以当前手势将被取消。
  178. },
  179. onShouldBlockNativeResponder: (evt, gestureState) => {
  180. // 返回一个布尔值,决定当前组件是否应该阻止原生组件成为JS响应者
  181. // 默认返回true。目前暂时只支持android。
  182. //基于业务交互场景,如果这里使用js事件处理,会导致容器不能左右滑动。所以设置成false.
  183. return false;
  184. }
  185. });
  186. if (this.state.show_controller) {
  187. this.time_hideController = setTimeout(() => {
  188. this.setState({
  189. show_controller: false
  190. // needback: false
  191. });
  192. this.bottomcontroller.setBottom(-50);
  193. this.topcontroller.setTop(-50);
  194. }, 5000);
  195. }
  196. }
  197. loadStart() {
  198. // alert("loadStart");
  199. }
  200. onBuffer({ isBuffering }: { isBuffering: boolean }) {
  201. //true为正在加载,false为没有加载,此处给loading提示
  202. this.setState({
  203. show_loading: isBuffering
  204. });
  205. }
  206. onLoad = data => {
  207. //获取的是秒数
  208. this.setState({ duration: data.duration });
  209. this.bottomcontroller.setMax(data.duration);
  210. };
  211. onProgress = data => {
  212. this.setState({
  213. currentTime: data.currentTime
  214. });
  215. if (this.state.show_controller) {
  216. this.bottomcontroller.setProgress(this.state.currentTime);
  217. this.bottomcontroller.setMax(this.state.duration);
  218. }
  219. };
  220. onError() {
  221. this.props.onError();
  222. }
  223. onEnd() {
  224. this.props.onEnd();
  225. }
  226. play() {
  227. //controller的play点击无法换图...
  228. if (this.state.paused) {
  229. this.start();
  230. } else {
  231. this.pause();
  232. }
  233. }
  234. pause() {
  235. this.setState({
  236. paused: true,
  237. player_status_icon: require("../images/video/start.png")
  238. });
  239. this.player_icon_index = 1;
  240. }
  241. start() {
  242. this.setState({
  243. paused: false,
  244. player_status_icon: require("../images/video/pause.png")
  245. });
  246. this.player_icon_index = 0;
  247. }
  248. seekTo(progress) {
  249. this.player.seek(progress);
  250. }
  251. presentFullscreenPlayer() {
  252. if (this.state.isFull) {
  253. this.setState({
  254. isFull: false,
  255. needback: this.props.needback
  256. });
  257. } else {
  258. this.setState({
  259. isFull: true,
  260. needback: true
  261. });
  262. }
  263. this.props.videofullScreenPlayer();
  264. this.bottomcontroller.setProgress(this.state.currentTime);
  265. }
  266. touch_up_callback(progress) {
  267. //抬起之后,获取算出来的progress
  268. this.setState({
  269. currentTime: progress
  270. });
  271. this.seekTo(progress);
  272. }
  273. refreshVideo() {
  274. this.setState({
  275. duration: 0,
  276. currentTime: 0
  277. });
  278. this.bottomcontroller.setProgress(0);
  279. }
  280. videoBackClick() {
  281. // if (this.props.needback != undefined && this.props.needback) {
  282. // }
  283. if (this.state.isFull) {
  284. //全屏状态下,变小屏
  285. this.presentFullscreenPlayer();
  286. } else {
  287. this.props.videoback();
  288. }
  289. }
  290. showController() {
  291. clearTimeout(this.time_hideController);
  292. this.setState({
  293. show_controller: true
  294. // needback: this.props.needback
  295. });
  296. this.bottomcontroller.setBottom(0);
  297. this.bottomcontroller.setProgress(this.state.currentTime);
  298. this.topcontroller.setTop(0);
  299. this.time_hideController = setTimeout(() => {
  300. this.setState({
  301. show_controller: false
  302. // needback: false
  303. });
  304. this.bottomcontroller.setBottom(-50);
  305. this.topcontroller.setTop(-50);
  306. }, 5000);
  307. }
  308. hideController() {
  309. this.setState({
  310. show_controller: false
  311. // needback: false
  312. });
  313. this.bottomcontroller.setBottom(-50);
  314. this.topcontroller.setTop(-50);
  315. }
  316. }
  317. /**
  318. * 将秒转换为 分:秒
  319. * s int 秒数
  320. */
  321. function formatTime(s) {
  322. //计算分钟
  323. //算法:将秒数除以60,然后下舍入,既得到分钟数
  324. var h;
  325. h = Math.floor(s / 60);
  326. //计算秒
  327. //算法:取得秒%60的余数,既得到秒数
  328. s = Math.round(s % 60);
  329. //将变量转换为字符串
  330. h += "";
  331. s += "";
  332. //如果只有一位数,前面增加一个0
  333. h = h.length == 1 ? "0" + h : h;
  334. s = s.length == 1 ? "0" + s : s;
  335. return h + ":" + s;
  336. }
  337. class TopController extends Component {
  338. state = {
  339. controller_top: 0
  340. };
  341. render() {
  342. if (this.props.needback == false) {
  343. return null;
  344. }
  345. return (
  346. <View
  347. style={[styles.player_controller, { top: this.state.controller_top }]}
  348. >
  349. <View
  350. style={{
  351. flex: 1,
  352. alignItems: "center"
  353. }}
  354. >
  355. <TouchableOpacity
  356. //点击事件放在这个组件里才管用
  357. style={{
  358. justifyContent: "center",
  359. width: "100%",
  360. height: "100%"
  361. }}
  362. onPress={() => this.props.videoback()}
  363. >
  364. <Image
  365. style={[styles.player_pause_icon]}
  366. source={require("../images/video/back.png")}
  367. />
  368. </TouchableOpacity>
  369. </View>
  370. <View style={{ flex: 2 }} />
  371. <View
  372. style={{
  373. flex: 9,
  374. overflow: "hidden"
  375. }}
  376. />
  377. <View style={{ flex: 2 }} />
  378. <View
  379. style={{
  380. flex: 1,
  381. alignItems: "center"
  382. }}
  383. />
  384. </View>
  385. );
  386. }
  387. setTop(top) {
  388. this.setState({
  389. controller_top: top
  390. });
  391. }
  392. }
  393. class Loading extends Component {
  394. constructor(props) {
  395. super(props);
  396. this.loading_index = 0;
  397. }
  398. state = {
  399. loading_img: this.props.loading_arr[0]
  400. };
  401. render() {
  402. if (this.props.show) {
  403. return (
  404. <Image
  405. source={this.state.loading_img}
  406. style={{ width: 100, height: 20 }}
  407. />
  408. );
  409. } else {
  410. return null;
  411. }
  412. }
  413. componentWillMount() {
  414. this.changeIndex();
  415. }
  416. changeIndex() {
  417. setTimeout(() => {
  418. if (this.loading_index > this.props.loading_arr.length - 2) {
  419. this.loading_index = 0;
  420. } else {
  421. this.loading_index = this.loading_index + 1;
  422. }
  423. this.setState({
  424. loading_img: this.props.loading_arr[this.loading_index]
  425. });
  426. this.changeIndex();
  427. }, 1);
  428. }
  429. }
  430. class BottomController extends Component {
  431. state = {
  432. controller_bottom: 0
  433. };
  434. render() {
  435. return (
  436. <View
  437. style={[
  438. styles.player_controller,
  439. { bottom: this.state.controller_bottom }
  440. ]}
  441. >
  442. <View
  443. style={{
  444. flex: 1,
  445. alignItems: "center"
  446. }}
  447. >
  448. {/* 暂停播放按钮 */}
  449. <TouchableOpacity
  450. //点击事件放在这个组件里才管用
  451. style={{
  452. justifyContent: "center",
  453. width: "100%",
  454. height: "100%"
  455. }}
  456. onPress={() => this.props.play_click()}
  457. >
  458. <Image
  459. style={[styles.player_pause_icon]}
  460. source={this.props.player_status_icon}
  461. />
  462. </TouchableOpacity>
  463. </View>
  464. <View style={{ flex: 2 }}>
  465. {/* //左侧当前播放时长 */}
  466. <Text style={[styles.player_time]}>
  467. {formatTime(this.props.currentTime)}
  468. </Text>
  469. </View>
  470. <View
  471. style={{
  472. flex: 9,
  473. overflow: "hidden"
  474. }}
  475. >
  476. {/* //中间进度条 */}
  477. <SeekBar
  478. style={{ flex: 1 }}
  479. ref={view => (this.seekbar = view)}
  480. //必须带此方法,作为滑动之后抬起的回调
  481. touchUpCallBack={this.props.touch_up_callback}
  482. />
  483. </View>
  484. <View style={{ flex: 2 }}>
  485. {/* //右侧总时长 */}
  486. <Text style={[styles.player_time]}>
  487. {formatTime(this.props.duration)}
  488. </Text>
  489. </View>
  490. <View
  491. style={{
  492. flex: 1,
  493. alignItems: "center"
  494. }}
  495. >
  496. <TouchableOpacity
  497. style={{
  498. justifyContent: "center",
  499. width: "100%",
  500. height: "100%"
  501. }}
  502. onPress={() => this.props.full_click()}
  503. >
  504. {/* //放大按钮 */}
  505. <Image
  506. style={[styles.fullscreen_icon]}
  507. source={require("../images/video/fullscreen.png")}
  508. />
  509. </TouchableOpacity>
  510. </View>
  511. </View>
  512. );
  513. }
  514. setMax(duration) {
  515. this.seekbar.setMax(duration);
  516. }
  517. getMax() {
  518. return this.seekbar.getMax();
  519. }
  520. setBottom(bottom) {
  521. this.setState({
  522. controller_bottom: bottom
  523. });
  524. }
  525. setProgress(currentTime) {
  526. this.seekbar.setProgress(currentTime);
  527. }
  528. }
  529. const styles = StyleSheet.create({
  530. player_controller: {
  531. flexDirection: "row",
  532. position: "absolute",
  533. width: "100%",
  534. height: 50,
  535. alignItems: "center",
  536. justifyContent: "center"
  537. },
  538. player_pause_icon: {
  539. width: "80%",
  540. resizeMode: "center",
  541. height: "40%",
  542. justifyContent: "center",
  543. alignItems: "center",
  544. left: 5
  545. },
  546. fullscreen_icon: {
  547. width: "80%",
  548. resizeMode: "center",
  549. height: "30%",
  550. justifyContent: "center",
  551. alignItems: "center"
  552. },
  553. player_time: {
  554. fontSize: 18,
  555. color: "white",
  556. alignItems: "center",
  557. justifyContent: "center",
  558. textAlignVertical: "center",
  559. textAlign: "center"
  560. }
  561. });
  562. /**
  563. 使用方法
  564. <CusVideo
  565. show={this.state.video_show} //是否显示
  566. uri={this.state.video_uri} //播放路径
  567. ref={view => (this.video = view)} //设置ID
  568. needback={false}
  569. videoback={() => alert("videoback")}
  570. videofullScreenPlayer={this.fullScreenPlayer.bind(this)}
  571. style={{
  572. left: this.state.x,
  573. top: this.state.y,
  574. width: this.state.video_width,
  575. height: this.state.video_height,
  576. overflow: "hidden",
  577. position: "absolute"
  578. }}
  579. />
  580. */