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

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