Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AbstractAudio.js 3.0KB

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