Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

VideoManager.tsx 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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<typeof 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. // @ts-ignore
  74. this.player.seek(time);
  75. }
  76. }
  77. /**
  78. * Plays video.
  79. *
  80. * @returns {void}
  81. */
  82. play() {
  83. this.setState({
  84. paused: false
  85. });
  86. }
  87. /**
  88. * Pauses video.
  89. *
  90. * @returns {void}
  91. */
  92. pause() {
  93. this.setState({
  94. paused: true
  95. });
  96. }
  97. /**
  98. * Handles playback rate changed event.
  99. *
  100. * @param {Object} options.playbackRate - Playback rate: 1 - playing, 0 - paused, other - slowed down / sped up.
  101. * @returns {void}
  102. */
  103. onPlaybackRateChange({ playbackRate }: { playbackRate: number; }) {
  104. if (playbackRate === 0) {
  105. this.setState({
  106. paused: true
  107. }, () => {
  108. this.onPause();
  109. });
  110. }
  111. if (playbackRate === 1) {
  112. this.setState({
  113. paused: false
  114. }, () => {
  115. this.onPlay();
  116. });
  117. }
  118. }
  119. /**
  120. * Handles progress update event.
  121. *
  122. * @param {Object} options - Progress event options.
  123. * @returns {void}
  124. */
  125. onProgress(options: { currentTime: number; }) {
  126. this.setState({ currentTime: options.currentTime });
  127. this.throttledFireUpdateSharedVideoEvent();
  128. }
  129. /**
  130. * Retrieves video tag params.
  131. *
  132. * @returns {void}
  133. */
  134. getPlayerOptions() {
  135. const { _isOwner, videoId, width, height } = this.props;
  136. const { paused } = this.state;
  137. const options: any = {
  138. paused,
  139. progressUpdateInterval: 5000,
  140. resizeMode: 'cover' as const,
  141. style: {
  142. height,
  143. width
  144. },
  145. source: { uri: videoId },
  146. controls: _isOwner,
  147. pictureInPicture: false,
  148. onProgress: this.onProgress,
  149. onError: (event: Error) => {
  150. logger.error('Error in the player:', event);
  151. }
  152. };
  153. if (_isOwner) {
  154. options.onPlaybackRateChange = this.onPlaybackRateChange;
  155. }
  156. return options;
  157. }
  158. /**
  159. * Implements React Component's render.
  160. *
  161. * @inheritdoc
  162. */
  163. render() {
  164. return (
  165. <Video
  166. ref = { this.playerRef }
  167. { ...this.getPlayerOptions() } />
  168. );
  169. }
  170. }
  171. export default connect(_mapStateToProps)(VideoManager);