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.

AbstractAudio.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // @flow
  2. import { Component } from 'react';
  3. import logger from '../logger';
  4. /**
  5. * Describes audio element interface used in the base/media feature for audio
  6. * playback.
  7. */
  8. export type AudioElement = {
  9. currentTime: number,
  10. pause: () => void,
  11. play: () => void,
  12. setSinkId?: string => Function,
  13. stop: () => void
  14. };
  15. /**
  16. * {@code AbstractAudio} component's property types.
  17. */
  18. type Props = {
  19. /**
  20. * A callback which will be called with {@code AbstractAudio} instance once
  21. * the audio element is loaded.
  22. */
  23. setRef?: ?AudioElement => void,
  24. /**
  25. * The URL of a media resource to use in the element.
  26. *
  27. * NOTE on react-native sound files are imported through 'require' and then
  28. * passed as the 'src' parameter which means their type will be 'any'.
  29. *
  30. * @type {Object | string}
  31. */
  32. src: Object | string,
  33. stream?: Object,
  34. loop?: ?boolean
  35. }
  36. /**
  37. * The React {@link Component} which is similar to Web's
  38. * {@code HTMLAudioElement}.
  39. */
  40. export default class AbstractAudio extends Component<Props> {
  41. /**
  42. * The {@link AudioElement} instance which implements the audio playback
  43. * functionality.
  44. */
  45. _audioElementImpl: ?AudioElement;
  46. /**
  47. * Initializes a new {@code AbstractAudio} instance.
  48. *
  49. * @param {Props} props - The read-only properties with which the new
  50. * instance is to be initialized.
  51. */
  52. constructor(props: Props) {
  53. super(props);
  54. // Bind event handlers so they are only bound once per instance.
  55. this.setAudioElementImpl = this.setAudioElementImpl.bind(this);
  56. }
  57. /**
  58. * Attempts to pause the playback of the media.
  59. *
  60. * @public
  61. * @returns {void}
  62. */
  63. pause(): void {
  64. this._audioElementImpl && this._audioElementImpl.pause();
  65. }
  66. /**
  67. * Attempts to begin the playback of the media.
  68. *
  69. * @public
  70. * @returns {void}
  71. */
  72. play(): void {
  73. this._audioElementImpl && this._audioElementImpl.play();
  74. }
  75. setAudioElementImpl: ?AudioElement => void;
  76. /**
  77. * Set the (reference to the) {@link AudioElement} object which implements
  78. * the audio playback functionality.
  79. *
  80. * @param {AudioElement} element - The {@link AudioElement} instance
  81. * which implements the audio playback functionality.
  82. * @protected
  83. * @returns {void}
  84. */
  85. setAudioElementImpl(element: ?AudioElement): void {
  86. this._audioElementImpl = element;
  87. // setRef
  88. const { setRef } = this.props;
  89. // $FlowFixMe
  90. typeof setRef === 'function' && setRef(element ? this : null);
  91. }
  92. /**
  93. * Sets the sink ID (output device ID) on the underlying audio element.
  94. * NOTE: Currently, implemented only on Web.
  95. *
  96. * @param {string} sinkId - The sink ID (output device ID).
  97. * @returns {void}
  98. */
  99. setSinkId(sinkId: string): void {
  100. this._audioElementImpl
  101. && typeof this._audioElementImpl.setSinkId === 'function'
  102. && this._audioElementImpl.setSinkId(sinkId)
  103. .catch(error => logger.error('Error setting sink', error));
  104. }
  105. /**
  106. * Attempts to stop the playback of the media.
  107. *
  108. * @public
  109. * @returns {void}
  110. */
  111. stop(): void {
  112. this._audioElementImpl && this._audioElementImpl.stop();
  113. }
  114. }