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

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