Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

VideoManager.tsx 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import React, { RefObject } from 'react';
  2. import Video from 'react-native-video';
  3. import { connect } from 'react-redux';
  4. import { PLAYBACK_STATUSES } from '../../constants';
  5. import logger from '../../logger';
  6. import AbstractVideoManager, {
  7. IProps,
  8. _mapStateToProps
  9. } from './AbstractVideoManager';
  10. interface IState {
  11. currentTime: number;
  12. paused: boolean;
  13. }
  14. /**
  15. * Manager of shared video.
  16. */
  17. class VideoManager extends AbstractVideoManager<IState> {
  18. playerRef: RefObject<Video>;
  19. /**
  20. * Initializes a new VideoManager instance.
  21. *
  22. * @param {Object} props - This component's props.
  23. *
  24. * @returns {void}
  25. */
  26. constructor(props: IProps) {
  27. super(props);
  28. this.state = {
  29. currentTime: 0,
  30. paused: false
  31. };
  32. this.playerRef = React.createRef();
  33. this.onPlaybackRateChange = this.onPlaybackRateChange.bind(this);
  34. this.onProgress = this.onProgress.bind(this);
  35. }
  36. /**
  37. * Retrieves the current player ref.
  38. */
  39. get player() {
  40. return this.playerRef.current;
  41. }
  42. /**
  43. * Indicates the playback state of the video.
  44. *
  45. * @returns {string}
  46. */
  47. getPlaybackStatus() {
  48. let status;
  49. if (this.state.paused) {
  50. status = PLAYBACK_STATUSES.PAUSED;
  51. } else {
  52. status = PLAYBACK_STATUSES.PLAYING;
  53. }
  54. return status;
  55. }
  56. /**
  57. * Retrieves current time.
  58. *
  59. * @returns {number}
  60. */
  61. getTime() {
  62. return this.state.currentTime;
  63. }
  64. /**
  65. * Seeks video to provided time.
  66. *
  67. * @param {number} time - The time to seek to.
  68. *
  69. * @returns {void}
  70. */
  71. seek(time: number) {
  72. if (this.player) {
  73. this.player.seek(time);
  74. }
  75. }
  76. /**
  77. * Plays video.
  78. *
  79. * @returns {void}
  80. */
  81. play() {
  82. this.setState({
  83. paused: false
  84. });
  85. }
  86. /**
  87. * Pauses video.
  88. *
  89. * @returns {void}
  90. */
  91. pause() {
  92. this.setState({
  93. paused: true
  94. });
  95. }
  96. /**
  97. * Handles playback rate changed event.
  98. *
  99. * @param {Object} options.playbackRate - Playback rate: 1 - playing, 0 - paused, other - slowed down / sped up.
  100. * @returns {void}
  101. */
  102. onPlaybackRateChange({ playbackRate }: { playbackRate: number; }) {
  103. if (playbackRate === 0) {
  104. this.setState({
  105. paused: true
  106. }, () => {
  107. this.onPause();
  108. });
  109. }
  110. if (playbackRate === 1) {
  111. this.setState({
  112. paused: false
  113. }, () => {
  114. this.onPlay();
  115. });
  116. }
  117. }
  118. /**
  119. * Handles progress update event.
  120. *
  121. * @param {Object} options - Progress event options.
  122. * @returns {void}
  123. */
  124. onProgress(options: { currentTime: number; }) {
  125. this.setState({ currentTime: options.currentTime });
  126. this.throttledFireUpdateSharedVideoEvent();
  127. }
  128. /**
  129. * Retrieves video tag params.
  130. *
  131. * @returns {void}
  132. */
  133. getPlayerOptions() {
  134. const { _isOwner, videoId, width, height } = this.props;
  135. const { paused } = this.state;
  136. const options: any = {
  137. paused,
  138. progressUpdateInterval: 5000,
  139. resizeMode: 'cover' as const,
  140. style: {
  141. height,
  142. width
  143. },
  144. source: { uri: videoId },
  145. controls: _isOwner,
  146. pictureInPicture: false,
  147. onProgress: this.onProgress,
  148. onError: (event: Error) => {
  149. logger.error('Error in the player:', event);
  150. }
  151. };
  152. if (_isOwner) {
  153. options.onPlaybackRateChange = this.onPlaybackRateChange;
  154. }
  155. return options;
  156. }
  157. /**
  158. * Implements React Component's render.
  159. *
  160. * @inheritdoc
  161. */
  162. render() {
  163. return (
  164. <Video
  165. ref = { this.playerRef }
  166. { ...this.getPlayerOptions() } />
  167. );
  168. }
  169. }
  170. export default connect(_mapStateToProps)(VideoManager);