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.

YoutubeVideoManager.tsx 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import React, { RefObject } from 'react';
  2. import Video from 'react-native-youtube-iframe';
  3. import { connect } from 'react-redux';
  4. import { PLAYBACK_STATUSES } from '../../constants';
  5. import AbstractVideoManager, {
  6. IProps,
  7. _mapStateToProps
  8. } from './AbstractVideoManager';
  9. /**
  10. * Passed to the webviewProps in order to avoid the usage of the ios player on which we cannot hide the controls.
  11. *
  12. * @private
  13. */
  14. const webviewUserAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'; // eslint-disable-line max-len
  15. interface IState {
  16. paused: boolean;
  17. }
  18. /**
  19. * Manager of youtube shared video.
  20. */
  21. class YoutubeVideoManager extends AbstractVideoManager<IState> {
  22. playerRef: RefObject<typeof Video>;
  23. /**
  24. * Initializes a new VideoManager instance.
  25. *
  26. * @param {Object} props - This component's props.
  27. *
  28. * @returns {void}
  29. */
  30. constructor(props: IProps) {
  31. super(props);
  32. this.state = {
  33. paused: false
  34. };
  35. this.playerRef = React.createRef();
  36. this._onReady = this._onReady.bind(this);
  37. this._onChangeState = this._onChangeState.bind(this);
  38. }
  39. /**
  40. * Retrieves the current player ref.
  41. */
  42. get player() {
  43. return this.playerRef.current;
  44. }
  45. /**
  46. * Indicates the playback state of the video.
  47. *
  48. * @returns {string}
  49. */
  50. getPlaybackStatus() {
  51. let status;
  52. if (this.state.paused) {
  53. status = PLAYBACK_STATUSES.PAUSED;
  54. } else {
  55. status = PLAYBACK_STATUSES.PLAYING;
  56. }
  57. return status;
  58. }
  59. /**
  60. * Retrieves current time.
  61. *
  62. * @returns {number}
  63. */
  64. getTime() {
  65. // @ts-ignore
  66. return this.player?.getCurrentTime();
  67. }
  68. /**
  69. * Seeks video to provided time.
  70. *
  71. * @param {number} time - The time to seek to.
  72. *
  73. * @returns {void}
  74. */
  75. seek(time: number) {
  76. if (this.player) {
  77. // @ts-ignore
  78. this.player.seekTo(time);
  79. }
  80. }
  81. /**
  82. * Plays video.
  83. *
  84. * @returns {void}
  85. */
  86. play() {
  87. this.setState({
  88. paused: false
  89. });
  90. }
  91. /**
  92. * Pauses video.
  93. *
  94. * @returns {void}
  95. */
  96. pause() {
  97. this.setState({
  98. paused: true
  99. });
  100. }
  101. /**
  102. * Handles state change event.
  103. *
  104. * @param {string} event - State event.
  105. * @returns {void}
  106. */
  107. _onChangeState(event: string) {
  108. if (event === 'paused') {
  109. this.setState({
  110. paused: true
  111. }, () => {
  112. this.onPause();
  113. });
  114. }
  115. if (event === 'playing') {
  116. this.setState({
  117. paused: false
  118. }, () => {
  119. this.onPlay();
  120. });
  121. }
  122. }
  123. /**
  124. * Handles onReady event.
  125. *
  126. * @returns {void}
  127. */
  128. _onReady() {
  129. this.setState({
  130. paused: false
  131. });
  132. }
  133. /**
  134. * Retrieves video tag params.
  135. *
  136. * @returns {void}
  137. */
  138. getPlayerOptions() {
  139. const { _isOwner, videoId, width, height } = this.props;
  140. const options: any = {
  141. height,
  142. initialPlayerParams: {
  143. controls: _isOwner,
  144. modestbranding: true,
  145. preventFullScreen: true
  146. },
  147. play: !this.state.paused,
  148. ref: this.playerRef,
  149. videoId,
  150. volume: 50,
  151. webViewProps: {
  152. bounces: false,
  153. mediaPlaybackRequiresUserAction: false,
  154. scrollEnabled: false,
  155. userAgent: webviewUserAgent
  156. },
  157. width
  158. };
  159. if (_isOwner) {
  160. options.onChangeState = this._onChangeState;
  161. options.onReady = this._onReady;
  162. }
  163. return options;
  164. }
  165. /**
  166. * Implements React Component's render.
  167. *
  168. * @inheritdoc
  169. */
  170. render() {
  171. return (
  172. <Video
  173. ref = { this.playerRef }
  174. { ...this.getPlayerOptions() } />
  175. );
  176. }
  177. }
  178. export default connect(_mapStateToProps)(YoutubeVideoManager);