You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

YoutubeVideoManager.js 4.0KB

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