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 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. play: Function,
  9. pause: Function
  10. }
  11. /**
  12. * {@code AbstractAudio} component's property types.
  13. */
  14. type Props = {
  15. /**
  16. * A callback which will be called with {@code AbstractAudio} instance once
  17. * the audio element is loaded.
  18. */
  19. setRef: ?Function,
  20. /**
  21. * The URL of a media resource to use in the element.
  22. *
  23. * NOTE on react-native sound files are imported through 'require' and then
  24. * passed as the 'src' parameter which means their type will be 'any'.
  25. *
  26. * @type {Object | string}
  27. */
  28. src: Object | string,
  29. stream: Object
  30. }
  31. /**
  32. * The React {@link Component} which is similar to Web's
  33. * {@code HTMLAudioElement}.
  34. */
  35. export default class AbstractAudio extends Component<Props> {
  36. /**
  37. * The {@link AudioElement} instance which implements the audio playback
  38. * functionality.
  39. */
  40. _audioElementImpl: ?AudioElement;
  41. /**
  42. * {@link setAudioElementImpl} bound to <code>this</code>.
  43. */
  44. setAudioElementImpl: Function;
  45. /**
  46. * Initializes a new {@code AbstractAudio} instance.
  47. *
  48. * @param {Object} props - The read-only properties with which the new
  49. * instance is to be initialized.
  50. */
  51. constructor(props: Object) {
  52. super(props);
  53. // Bind event handlers so they are only bound once for every 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() {
  63. this._audioElementImpl && this._audioElementImpl.pause();
  64. }
  65. /**
  66. * Attempts to being the playback of the media.
  67. *
  68. * @public
  69. * @returns {void}
  70. */
  71. play() {
  72. this._audioElementImpl && this._audioElementImpl.play();
  73. }
  74. /**
  75. * Set the (reference to the) {@link AudioElement} object which implements
  76. * the audio playback functionality.
  77. *
  78. * @param {AudioElement} element - The {@link AudioElement} instance
  79. * which implements the audio playback functionality.
  80. * @protected
  81. * @returns {void}
  82. */
  83. setAudioElementImpl(element: ?AudioElement) {
  84. this._audioElementImpl = element;
  85. if (typeof this.props.setRef === 'function') {
  86. this.props.setRef(element ? this : null);
  87. }
  88. }
  89. }