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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* eslint-disable no-invalid-this */
  2. import React from 'react';
  3. import YouTube from 'react-youtube';
  4. import { connect } from '../../../base/redux';
  5. import AbstractVideoManager, {
  6. _mapDispatchToProps,
  7. _mapStateToProps,
  8. PLAYBACK_STATES
  9. } from './AbstractVideoManager';
  10. /**
  11. * Manager of shared video.
  12. *
  13. * @returns {void}
  14. */
  15. class YoutubeVideoManager extends AbstractVideoManager<Props> {
  16. /**
  17. * Initializes a new YoutubeVideoManager instance.
  18. *
  19. * @param {Object} props - This component's props.
  20. *
  21. * @returns {void}
  22. */
  23. constructor(props) {
  24. super(props);
  25. this.isPlayerAPILoaded = false;
  26. }
  27. /**
  28. * Indicates the playback state of the video.
  29. *
  30. * @returns {string}
  31. */
  32. getPlaybackState() {
  33. let state;
  34. if (!this.player) {
  35. return;
  36. }
  37. const playerState = this.player.getPlayerState();
  38. if (playerState === YouTube.PlayerState.PLAYING) {
  39. state = PLAYBACK_STATES.PLAYING;
  40. }
  41. if (playerState === YouTube.PlayerState.PAUSED) {
  42. state = PLAYBACK_STATES.PAUSED;
  43. }
  44. return state;
  45. }
  46. /**
  47. * Indicates whether the video is muted.
  48. *
  49. * @returns {boolean}
  50. */
  51. isMuted() {
  52. return this.player?.isMuted();
  53. }
  54. /**
  55. * Retrieves current volume.
  56. *
  57. * @returns {number}
  58. */
  59. getVolume() {
  60. return this.player?.getVolume();
  61. }
  62. /**
  63. * Sets player volume.
  64. *
  65. * @param {number} value - The volume.
  66. *
  67. * @returns {void}
  68. */
  69. setVolume(value) {
  70. return this.player?.setVolume(value);
  71. }
  72. /**
  73. * Retrieves current time.
  74. *
  75. * @returns {number}
  76. */
  77. getTime() {
  78. return this.player?.getCurrentTime();
  79. }
  80. /**
  81. * Seeks video to provided time.
  82. *
  83. * @param {number} time - The time to seek to.
  84. *
  85. * @returns {void}
  86. */
  87. seek(time) {
  88. return this.player?.seekTo(time);
  89. }
  90. /**
  91. * Plays video.
  92. *
  93. * @returns {void}
  94. */
  95. play() {
  96. return this.player?.playVideo();
  97. }
  98. /**
  99. * Pauses video.
  100. *
  101. * @returns {void}
  102. */
  103. pause() {
  104. return this.player?.pauseVideo();
  105. }
  106. /**
  107. * Mutes video.
  108. *
  109. * @returns {void}
  110. */
  111. mute() {
  112. return this.player?.mute();
  113. }
  114. /**
  115. * Unmutes video.
  116. *
  117. * @returns {void}
  118. */
  119. unMute() {
  120. return this.player?.unMute();
  121. }
  122. /**
  123. * Disposes of the current video player.
  124. *
  125. * @returns {void}
  126. */
  127. dispose() {
  128. if (this.player) {
  129. this.player.destroy();
  130. this.player = null;
  131. }
  132. }
  133. /**
  134. * Fired on play state toggle.
  135. *
  136. * @param {Object} event - The yt player stateChange event.
  137. *
  138. * @returns {void}
  139. */
  140. onPlayerStateChange = event => {
  141. if (event.data === YouTube.PlayerState.PLAYING) {
  142. this.onPlay();
  143. } else if (event.data === YouTube.PlayerState.PAUSED) {
  144. this.onPause();
  145. }
  146. }
  147. /**
  148. * Fired when youtube player is ready.
  149. *
  150. * @param {Object} event - The youtube player event.
  151. *
  152. * @returns {void}
  153. */
  154. onPlayerReady = event => {
  155. const { _isOwner } = this.props;
  156. this.player = event.target;
  157. this.player.addEventListener('onVolumeChange', () => {
  158. this.onVolumeChange();
  159. });
  160. if (_isOwner) {
  161. this.player.addEventListener('onVideoProgress', this.throttledFireUpdateSharedVideoEvent);
  162. }
  163. this.play();
  164. // sometimes youtube can get muted state from previous videos played in the browser
  165. // and as we are disabling controls we want to unmute it
  166. if (this.isMuted()) {
  167. this.unMute();
  168. }
  169. };
  170. getPlayerOptions = () => {
  171. const { _isOwner, videoId } = this.props;
  172. const showControls = _isOwner ? 1 : 0;
  173. const options = {
  174. id: 'sharedVideoPlayer',
  175. opts: {
  176. height: '100%',
  177. width: '100%',
  178. playerVars: {
  179. 'origin': location.origin,
  180. 'fs': '0',
  181. 'autoplay': 0,
  182. 'controls': showControls,
  183. 'rel': 0
  184. }
  185. },
  186. onError: () => this.onError(),
  187. onReady: this.onPlayerReady,
  188. onStateChange: this.onPlayerStateChange,
  189. videoId
  190. };
  191. return options;
  192. }
  193. /**
  194. * Implements React Component's render.
  195. *
  196. * @inheritdoc
  197. */
  198. render() {
  199. return (<YouTube
  200. { ...this.getPlayerOptions() } />);
  201. }
  202. }
  203. export default connect(_mapStateToProps, _mapDispatchToProps)(YoutubeVideoManager);