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.

VideoManager.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import React from 'react';
  2. import { connect } from '../../../base/redux';
  3. import AbstractVideoManager, {
  4. _mapDispatchToProps,
  5. _mapStateToProps,
  6. PLAYBACK_STATES,
  7. Props
  8. } from './AbstractVideoManager';
  9. /**
  10. * Manager of shared video.
  11. */
  12. class VideoManager extends AbstractVideoManager<Props> {
  13. /**
  14. * Initializes a new VideoManager instance.
  15. *
  16. * @param {Object} props - This component's props.
  17. *
  18. * @returns {void}
  19. */
  20. constructor(props) {
  21. super(props);
  22. this.playerRef = React.createRef();
  23. }
  24. /**
  25. * Retrieves the current player ref.
  26. */
  27. get player() {
  28. return this.playerRef.current;
  29. }
  30. /**
  31. * Indicates the playback state of the video.
  32. *
  33. * @returns {string}
  34. */
  35. getPlaybackState() {
  36. let state;
  37. if (!this.player) {
  38. return;
  39. }
  40. if (this.player.paused) {
  41. state = PLAYBACK_STATES.PAUSED;
  42. } else {
  43. state = PLAYBACK_STATES.PLAYING;
  44. }
  45. return state;
  46. }
  47. /**
  48. * Indicates whether the video is muted.
  49. *
  50. * @returns {boolean}
  51. */
  52. isMuted() {
  53. return this.player?.muted;
  54. }
  55. /**
  56. * Retrieves current volume.
  57. *
  58. * @returns {number}
  59. */
  60. getVolume() {
  61. return this.player?.volume;
  62. }
  63. /**
  64. * Sets player volume.
  65. *
  66. * @param {number} value - The volume.
  67. *
  68. * @returns {void}
  69. */
  70. setVolume(value) {
  71. if (this.player) {
  72. this.player.volume = value;
  73. }
  74. }
  75. /**
  76. * Retrieves current time.
  77. *
  78. * @returns {number}
  79. */
  80. getTime() {
  81. return this.player?.currentTime;
  82. }
  83. /**
  84. * Seeks video to provided time.
  85. *
  86. * @param {number} time - The time to seek to.
  87. *
  88. * @returns {void}
  89. */
  90. seek(time) {
  91. if (this.player) {
  92. this.player.currentTime = time;
  93. }
  94. }
  95. /**
  96. * Plays video.
  97. *
  98. * @returns {void}
  99. */
  100. play() {
  101. return this.player?.play();
  102. }
  103. /**
  104. * Pauses video.
  105. *
  106. * @returns {void}
  107. */
  108. pause() {
  109. return this.player?.pause();
  110. }
  111. /**
  112. * Mutes video.
  113. *
  114. * @returns {void}
  115. */
  116. mute() {
  117. if (this.player) {
  118. this.player.muted = true;
  119. }
  120. }
  121. /**
  122. * Unmutes video.
  123. *
  124. * @returns {void}
  125. */
  126. unMute() {
  127. if (this.player) {
  128. this.player.muted = false;
  129. }
  130. }
  131. /**
  132. * Retrieves video tag params.
  133. *
  134. * @returns {void}
  135. */
  136. getPlayerOptions() {
  137. const { _isOwner, videoId } = this.props;
  138. let options = {
  139. autoPlay: true,
  140. src: videoId,
  141. controls: _isOwner,
  142. onError: () => this.onError(),
  143. onPlay: () => this.onPlay(),
  144. onVolumeChange: () => this.onVolumeChange()
  145. };
  146. if (_isOwner) {
  147. options = {
  148. ...options,
  149. onPause: () => this.onPause(),
  150. onTimeUpdate: this.throttledFireUpdateSharedVideoEvent
  151. };
  152. }
  153. return options;
  154. }
  155. /**
  156. * Implements React Component's render.
  157. *
  158. * @inheritdoc
  159. */
  160. render() {
  161. return (<video
  162. id = 'sharedVideoPlayer'
  163. ref = { this.playerRef }
  164. { ...this.getPlayerOptions() } />);
  165. }
  166. }
  167. export default connect(_mapStateToProps, _mapDispatchToProps)(VideoManager);