CusVideo.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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: true,
  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. resizeMode={this.state.resizeMode}
  41. rate={this.state.rate} //播放速率
  42. paused={this.state.paused} //暂停
  43. volume={this.state.volume} //调节音量
  44. muted={this.state.muted} //控制音频是否静音
  45. resizeMode={this.state.resizeMode} //缩放模式
  46. onLoadStart={this.loadStart} // 当视频开始加载时的回调函数
  47. onLoad={this.onLoad} //加载媒体并准备播放时调用的回调函数。
  48. onProgress={this.onProgress} //视频播放过程中每个间隔进度单位调用的回调函数
  49. onEnd={this.onEnd} //视频播放结束时的回调函数
  50. onAudioBecomingNoisy={this.onAudioBecomingNoisy} //音频变得嘈杂时的回调 - 应暂停视频
  51. onAudioFocusChanged={this.onAudioFocusChanged} //音频焦点丢失时的回调 - 如果焦点丢失则暂停
  52. repeat={this.state.wheel} //确定在到达结尾时是否重复播放视频。
  53. />
  54. <View style={[styles.player_controller]}>
  55. <View
  56. style={{
  57. flex: 1,
  58. alignItems: "center"
  59. }}
  60. >
  61. {/* 暂停播放按钮 */}
  62. <TouchableOpacity
  63. //点击事件放在这个组件里才管用
  64. style={{
  65. justifyContent: "center",
  66. width: "100%",
  67. height: "100%"
  68. }}
  69. onPress={() => this.play()}
  70. >
  71. <Image
  72. style={[styles.player_pause_icon]}
  73. //source={require("../imgs/player_image_state_pause.png")}
  74. />
  75. </TouchableOpacity>
  76. </View>
  77. <View style={{ flex: 3 }}>
  78. {/* //左侧当前播放时长 */}
  79. <Text style={[styles.player_time]}>
  80. {formatTime(this.state.currentTime)}
  81. </Text>
  82. </View>
  83. <View
  84. style={{
  85. flex: 7,
  86. backgroundColor: "yellow"
  87. }}
  88. >
  89. {/* //中间进度条 */}
  90. {/* <SeekBar
  91. style={{
  92. flex: 1,
  93. alignItems: "center",
  94. justifyContent: "center",
  95. width: "100%"
  96. }}
  97. max={this.state.duration}
  98. progress={this.state.currentTime}
  99. /> */}
  100. </View>
  101. <View style={{ flex: 3 }}>
  102. {/* //右侧总时长 */}
  103. <Text style={[styles.player_time]}>
  104. {formatTime(this.state.duration)}
  105. </Text>
  106. </View>
  107. </View>
  108. </View>
  109. );
  110. }
  111. loadStart() {
  112. // alert("准备加载视频");
  113. }
  114. onLoad = data => {
  115. //获取的是秒数
  116. this.setState({ duration: data.duration });
  117. };
  118. onProgress = data => {
  119. this.setState({
  120. currentTime: data.currentTime
  121. });
  122. };
  123. play() {
  124. this.setState({ paused: false });
  125. // if (this.state.paused) {
  126. // this.player_icon_index = 1;
  127. // } else {
  128. // this.player_icon_index = 0;
  129. // }
  130. }
  131. pause() {
  132. this.setState({ paused: true });
  133. }
  134. showToast(params) {
  135. // ToastExample.message(params);
  136. ToastExample.show(params, ToastExample.SHORT);
  137. }
  138. refreshVideo() {
  139. this.setState({ duration: 0, currentTime: 0 });
  140. }
  141. }
  142. const styles = StyleSheet.create({
  143. player_controller: {
  144. flexDirection: "row",
  145. position: "absolute",
  146. width: "100%",
  147. height: 50,
  148. alignItems: "center",
  149. bottom: 0,
  150. backgroundColor: "powderblue"
  151. },
  152. player_pause_icon: {
  153. width: "100%",
  154. resizeMode: "center",
  155. height: "80%",
  156. justifyContent: "center",
  157. alignItems: "center",
  158. backgroundColor: "red"
  159. },
  160. player_time: {
  161. backgroundColor: "red",
  162. fontSize: 30,
  163. alignItems: "center",
  164. justifyContent: "center",
  165. textAlignVertical: "center",
  166. textAlign: "center"
  167. }
  168. });
  169. /**
  170. * 将秒转换为 分:秒
  171. * s int 秒数
  172. */
  173. function formatTime(s) {
  174. //计算分钟
  175. //算法:将秒数除以60,然后下舍入,既得到分钟数
  176. var h;
  177. h = Math.floor(s / 60);
  178. //计算秒
  179. //算法:取得秒%60的余数,既得到秒数
  180. s = Math.round(s % 60);
  181. //将变量转换为字符串
  182. h += "";
  183. s += "";
  184. //如果只有一位数,前面增加一个0
  185. h = h.length == 1 ? "0" + h : h;
  186. s = s.length == 1 ? "0" + s : s;
  187. return h + ":" + s;
  188. }