CusVideo.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import React, { Component } from "react";
  2. import {
  3. Platform,
  4. Text,
  5. View,
  6. ImageBackground,
  7. Button,
  8. Image,
  9. TouchableOpacity,
  10. StyleSheet
  11. } from "react-native";
  12. import { createStackNavigator, createAppContainer } from "react-navigation";
  13. import Video from "react-native-video";
  14. const instructions = Platform.select({
  15. ios: "Press Cmd+R to reload,\n" + "Cmd+D or shake for dev menu",
  16. android:
  17. "Double tap R on your keyboard to reload,\n" +
  18. "Shake or press menu button for dev menu"
  19. });
  20. type Props = {};
  21. export default class CusVideo extends React.Component {
  22. state = {
  23. rate: 1,
  24. volume: 1,
  25. muted: false,
  26. resizeMode: "stretch",
  27. duration: 0.0,
  28. currentTime: 0.0,
  29. paused: false,
  30. wheel: false
  31. };
  32. render() {
  33. return (
  34. <View style={this.props.style}>
  35. <Video
  36. style={{ flex: 1 }}
  37. source={{
  38. uri: this.props.uri
  39. }}
  40. ref={ref => {
  41. this.player = ref;
  42. }}
  43. // poster={this.props.poster}
  44. resizeMode={this.state.resizeMode}
  45. // posterResizeMode={this.state.resizeMode}
  46. rate={this.state.rate} //播放速率
  47. paused={this.state.paused} //暂停
  48. volume={this.state.volume} //调节音量
  49. muted={this.state.muted} //控制音频是否静音
  50. resizeMode={this.state.resizeMode} //缩放模式
  51. onLoadStart={this.loadStart} // 当视频开始加载时的回调函数
  52. onLoad={this.onLoad} //加载媒体并准备播放时调用的回调函数。
  53. onProgress={this.onProgress} //视频播放过程中每个间隔进度单位调用的回调函数
  54. onEnd={this.onEnd} //视频播放结束时的回调函数
  55. onAudioBecomingNoisy={this.onAudioBecomingNoisy} //音频变得嘈杂时的回调 - 应暂停视频
  56. onAudioFocusChanged={this.onAudioFocusChanged} //音频焦点丢失时的回调 - 如果焦点丢失则暂停
  57. repeat={this.state.wheel} //确定在到达结尾时是否重复播放视频。
  58. onError={this.onError} // 当视频不能加载,或出错后的回调函数
  59. playInBackground={false} // 当app转到后台运行的时候,播放是否暂停
  60. playWhenInactive={true} // [iOS] Video continues to play when control or notification center are shown. 仅适用于IOS
  61. />
  62. <View style={[styles.player_controller]}>
  63. <View
  64. style={{
  65. flex: 1,
  66. alignItems: "center"
  67. }}
  68. >
  69. {/* 暂停播放按钮 */}
  70. <TouchableOpacity
  71. //点击事件放在这个组件里才管用
  72. style={{
  73. justifyContent: "center",
  74. width: "100%",
  75. height: "100%"
  76. }}
  77. onPress={() => this.play()}
  78. >
  79. <Image
  80. style={[styles.player_pause_icon]}
  81. //source={require("../imgs/player_image_state_pause.png")}
  82. />
  83. </TouchableOpacity>
  84. </View>
  85. <View style={{ flex: 3 }}>
  86. {/* //左侧当前播放时长 */}
  87. <Text style={[styles.player_time]}>
  88. {formatTime(this.state.currentTime)}
  89. </Text>
  90. </View>
  91. <View
  92. style={{
  93. flex: 7,
  94. backgroundColor: "yellow"
  95. }}
  96. >
  97. {/* //中间进度条 */}
  98. {/* <SeekBar
  99. style={{
  100. flex: 1,
  101. alignItems: "center",
  102. justifyContent: "center",
  103. width: "100%"
  104. }}
  105. max={this.state.duration}
  106. progress={this.state.currentTime}
  107. /> */}
  108. </View>
  109. <View style={{ flex: 3 }}>
  110. {/* //右侧总时长 */}
  111. <Text style={[styles.player_time]}>
  112. {formatTime(this.state.duration)}
  113. </Text>
  114. </View>
  115. <TouchableOpacity
  116. style={{ flex: 1 }}
  117. onPress={() => this.presentFullscreenPlayer()}
  118. >
  119. <View style={{ flex: 1 }}>
  120. {/* //放大按钮 */}
  121. <Image style={[styles.player_pause_icon]} />
  122. </View>
  123. </TouchableOpacity>
  124. </View>
  125. </View>
  126. );
  127. }
  128. loadStart() {
  129. // alert("loadStart");
  130. }
  131. onLoad = data => {
  132. //获取的是秒数
  133. this.setState({ duration: data.duration });
  134. };
  135. onProgress = data => {
  136. this.setState({
  137. currentTime: data.currentTime
  138. });
  139. };
  140. onError() {
  141. alert("播放器异常");
  142. }
  143. onEnd() {
  144. alert("播放结束");
  145. }
  146. play() {
  147. if (this.state.paused) {
  148. this.start();
  149. } else {
  150. this.pause();
  151. }
  152. }
  153. pause() {
  154. this.setState({ paused: true });
  155. this.player_icon_index = 1;
  156. }
  157. start() {
  158. this.setState({ paused: false });
  159. this.player_icon_index = 0;
  160. }
  161. showToast(params) {
  162. // ToastExample.message(params);
  163. ToastExample.show(params, ToastExample.SHORT);
  164. }
  165. presentFullscreenPlayer() {
  166. // alert("点击调用全屏");
  167. this.props.videofullScreenPlayer();
  168. // this.player.presentFullscreenPlayer();
  169. }
  170. refreshVideo() {
  171. this.setState({
  172. duration: 0,
  173. currentTime: 0
  174. });
  175. }
  176. }
  177. const styles = StyleSheet.create({
  178. player_controller: {
  179. flexDirection: "row",
  180. position: "absolute",
  181. width: "100%",
  182. height: 50,
  183. alignItems: "center",
  184. bottom: 0
  185. },
  186. player_pause_icon: {
  187. width: "100%",
  188. resizeMode: "center",
  189. height: "80%",
  190. justifyContent: "center",
  191. alignItems: "center",
  192. backgroundColor: "blue"
  193. },
  194. player_time: {
  195. fontSize: 18,
  196. color: "white",
  197. alignItems: "center",
  198. justifyContent: "center",
  199. textAlignVertical: "center",
  200. textAlign: "center"
  201. }
  202. });
  203. /**
  204. * 将秒转换为 分:秒
  205. * s int 秒数
  206. */
  207. function formatTime(s) {
  208. //计算分钟
  209. //算法:将秒数除以60,然后下舍入,既得到分钟数
  210. var h;
  211. h = Math.floor(s / 60);
  212. //计算秒
  213. //算法:取得秒%60的余数,既得到秒数
  214. s = Math.round(s % 60);
  215. //将变量转换为字符串
  216. h += "";
  217. s += "";
  218. //如果只有一位数,前面增加一个0
  219. h = h.length == 1 ? "0" + h : h;
  220. s = s.length == 1 ? "0" + s : s;
  221. return h + ":" + s;
  222. }