CusVideo.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 style={{ flex: 7, backgroundColor: "yellow" }}>
  84. {/* //中间进度条 */}
  85. {/* <SeekBar
  86. style={{
  87. flex: 1,
  88. alignItems: "center",
  89. justifyContent: "center",
  90. width: "100%"
  91. }}
  92. max={this.state.duration}
  93. progress={this.state.currentTime}
  94. /> */}
  95. </View>
  96. <View style={{ flex: 3 }}>
  97. {/* //右侧总时长 */}
  98. <Text style={[styles.player_time]}>
  99. {formatTime(this.state.duration)}
  100. </Text>
  101. </View>
  102. </View>
  103. </View>
  104. );
  105. }
  106. loadStart() {
  107. // alert("准备加载视频");
  108. }
  109. onLoad = data => {
  110. //获取的是秒数
  111. this.setState({ duration: data.duration });
  112. };
  113. onProgress = data => {
  114. this.setState({
  115. currentTime: data.currentTime
  116. });
  117. };
  118. play() {
  119. this.setState({ paused: !this.state.paused });
  120. // if (this.state.paused) {
  121. // this.player_icon_index = 1;
  122. // } else {
  123. // this.player_icon_index = 0;
  124. // }
  125. }
  126. showToast(params) {
  127. // ToastExample.message(params);
  128. ToastExample.show(params, ToastExample.SHORT);
  129. }
  130. }
  131. const styles = StyleSheet.create({
  132. player_controller: {
  133. flexDirection: "row",
  134. position: "absolute",
  135. width: "100%",
  136. height: 50,
  137. alignItems: "center",
  138. bottom: 0,
  139. backgroundColor: "powderblue"
  140. },
  141. player_pause_icon: {
  142. width: "100%",
  143. resizeMode: "center",
  144. height: "80%",
  145. justifyContent: "center",
  146. alignItems: "center",
  147. backgroundColor: "red"
  148. },
  149. player_time: {
  150. backgroundColor: "red",
  151. fontSize: 30,
  152. alignItems: "center",
  153. justifyContent: "center",
  154. textAlignVertical: "center",
  155. textAlign: "center"
  156. }
  157. });
  158. /**
  159. * 将秒转换为 分:秒
  160. * s int 秒数
  161. */
  162. function formatTime(s) {
  163. //计算分钟
  164. //算法:将秒数除以60,然后下舍入,既得到分钟数
  165. var h;
  166. h = Math.floor(s / 60);
  167. //计算秒
  168. //算法:取得秒%60的余数,既得到秒数
  169. s = Math.round(s % 60);
  170. //将变量转换为字符串
  171. h += "";
  172. s += "";
  173. //如果只有一位数,前面增加一个0
  174. h = h.length == 1 ? "0" + h : h;
  175. s = s.length == 1 ? "0" + s : s;
  176. return h + ":" + s;
  177. }