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.

AudioTrack.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // @flow
  2. import React, { Component } from 'react';
  3. /**
  4. * The type of the React {@code Component} props of {@link AudioTrack}.
  5. */
  6. type Props = {
  7. /**
  8. * The value of the id attribute of the audio element.
  9. */
  10. id: string,
  11. /**
  12. * The audio track.
  13. */
  14. audioTrack: ?Object,
  15. /**
  16. * Used to determine the value of the autoplay attribute of the underlying
  17. * audio element.
  18. */
  19. autoPlay: boolean,
  20. /**
  21. * Represents muted property of the underlying audio element.
  22. */
  23. muted: ?Boolean,
  24. /**
  25. * Represents volume property of the underlying audio element.
  26. */
  27. volume: ?number,
  28. /**
  29. * A function that will be executed when the reference to the underlying audio element changes in order to report
  30. * the initial volume value.
  31. */
  32. onInitialVolumeSet: Function
  33. };
  34. /**
  35. * The React/Web {@link Component} which is similar to and wraps around {@code HTMLAudioElement}.
  36. */
  37. export default class AudioTrack extends Component<Props> {
  38. /**
  39. * Reference to the HTML audio element, stored until the file is ready.
  40. */
  41. _ref: ?HTMLAudioElement;
  42. /**
  43. * Default values for {@code AudioTrack} component's properties.
  44. *
  45. * @static
  46. */
  47. static defaultProps = {
  48. autoPlay: true,
  49. id: ''
  50. };
  51. /**
  52. * Creates new <code>Audio</code> element instance with given props.
  53. *
  54. * @param {Object} props - The read-only properties with which the new
  55. * instance is to be initialized.
  56. */
  57. constructor(props: Props) {
  58. super(props);
  59. // Bind event handlers so they are only bound once for every instance.
  60. this._setRef = this._setRef.bind(this);
  61. }
  62. /**
  63. * Attaches the audio track to the audio element and plays it.
  64. *
  65. * @inheritdoc
  66. * @returns {void}
  67. */
  68. componentDidMount() {
  69. this._attachTrack(this.props.audioTrack);
  70. if (this._ref) {
  71. const { autoPlay, muted, volume } = this.props;
  72. if (autoPlay) {
  73. // Ensure the audio gets play() called on it. This may be necessary in the
  74. // case where the local video container was moved and re-attached, in which
  75. // case the audio may not autoplay.
  76. this._ref.play();
  77. }
  78. if (typeof volume === 'number') {
  79. this._ref.volume = volume;
  80. }
  81. if (typeof muted === 'boolean') {
  82. this._ref.muted = muted;
  83. }
  84. }
  85. }
  86. /**
  87. * Remove any existing associations between the current audio track and the
  88. * component's audio element.
  89. *
  90. * @inheritdoc
  91. * @returns {void}
  92. */
  93. componentWillUnmount() {
  94. this._detachTrack(this.props.audioTrack);
  95. }
  96. /**
  97. * This component's updating is blackboxed from React to prevent re-rendering of the audio
  98. * element, as we set all the properties manually.
  99. *
  100. * @inheritdoc
  101. * @returns {boolean} - False is always returned to blackbox this component
  102. * from React.
  103. */
  104. shouldComponentUpdate(nextProps: Props) {
  105. const currentJitsiTrack = this.props.audioTrack?.jitsiTrack;
  106. const nextJitsiTrack = nextProps.audioTrack?.jitsiTrack;
  107. if (currentJitsiTrack !== nextJitsiTrack) {
  108. this._detachTrack(this.props.audioTrack);
  109. this._attachTrack(nextProps.audioTrack);
  110. }
  111. if (this._ref) {
  112. const currentVolume = this._ref.volume;
  113. const nextVolume = nextProps.volume;
  114. if (typeof nextVolume === 'number' && !isNaN(nextVolume) && currentVolume !== nextVolume) {
  115. this._ref.volume = nextVolume;
  116. }
  117. const currentMuted = this._ref.muted;
  118. const nextMuted = nextProps.muted;
  119. if (typeof nextMuted === 'boolean' && currentMuted !== nextVolume) {
  120. this._ref.muted = nextMuted;
  121. }
  122. }
  123. return false;
  124. }
  125. /**
  126. * Implements React's {@link Component#render()}.
  127. *
  128. * @inheritdoc
  129. * @returns {ReactElement}
  130. */
  131. render() {
  132. const { autoPlay, id } = this.props;
  133. return (
  134. <audio
  135. autoPlay = { autoPlay }
  136. id = { id }
  137. ref = { this._setRef } />
  138. );
  139. }
  140. /**
  141. * Calls into the passed in track to associate the track with the component's audio element.
  142. *
  143. * @param {Object} track - The redux representation of the {@code JitsiLocalTrack}.
  144. * @private
  145. * @returns {void}
  146. */
  147. _attachTrack(track) {
  148. if (!track || !track.jitsiTrack) {
  149. return;
  150. }
  151. track.jitsiTrack.attach(this._ref);
  152. }
  153. /**
  154. * Removes the association to the component's audio element from the passed
  155. * in redux representation of jitsi audio track.
  156. *
  157. * @param {Object} track - The redux representation of the {@code JitsiLocalTrack}.
  158. * @private
  159. * @returns {void}
  160. */
  161. _detachTrack(track) {
  162. if (this._ref && track && track.jitsiTrack) {
  163. track.jitsiTrack.detach(this._ref);
  164. }
  165. }
  166. _setRef: (?HTMLAudioElement) => void;
  167. /**
  168. * Sets the reference to the HTML audio element.
  169. *
  170. * @param {HTMLAudioElement} audioElement - The HTML audio element instance.
  171. * @private
  172. * @returns {void}
  173. */
  174. _setRef(audioElement: ?HTMLAudioElement) {
  175. this._ref = audioElement;
  176. const { onInitialVolumeSet } = this.props;
  177. if (this._ref && onInitialVolumeSet) {
  178. onInitialVolumeSet(this._ref.volume);
  179. }
  180. }
  181. }