Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

AbstractAudio.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. };
  13. /**
  14. * {@code AbstractAudio} component's property types.
  15. */
  16. type Props = {
  17. /**
  18. * A callback which will be called with {@code AbstractAudio} instance once
  19. * the audio element is loaded.
  20. */
  21. setRef?: ?AudioElement => void,
  22. /**
  23. * The URL of a media resource to use in the element.
  24. *
  25. * NOTE on react-native sound files are imported through 'require' and then
  26. * passed as the 'src' parameter which means their type will be 'any'.
  27. *
  28. * @type {Object | string}
  29. */
  30. src: Object | string,
  31. stream: Object,
  32. loop?: ?boolean
  33. }
  34. /**
  35. * The React {@link Component} which is similar to Web's
  36. * {@code HTMLAudioElement}.
  37. */
  38. export default class AbstractAudio extends Component<Props> {
  39. /**
  40. * The {@link AudioElement} instance which implements the audio playback
  41. * functionality.
  42. */
  43. _audioElementImpl: ?AudioElement;
  44. /**
  45. * Initializes a new {@code AbstractAudio} instance.
  46. *
  47. * @param {Props} props - The read-only properties with which the new
  48. * instance is to be initialized.
  49. */
  50. constructor(props: Props) {
  51. super(props);
  52. // Bind event handlers so they are only bound once per instance.
  53. this.setAudioElementImpl = this.setAudioElementImpl.bind(this);
  54. }
  55. pause: () => void;
  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. play: () => void;
  66. /**
  67. * Attempts to being 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. setSinkId: string => void;
  93. /**
  94. * Sets the sink ID (output device ID) on the underlying audio element.
  95. * NOTE: Currently, implemented only on Web.
  96. *
  97. * @param {string} sinkId - The sink ID (output device ID).
  98. * @returns {void}
  99. */
  100. setSinkId(sinkId: string): void {
  101. this._audioElementImpl
  102. && typeof this._audioElementImpl.setSinkId === 'function'
  103. && this._audioElementImpl.setSinkId(sinkId);
  104. }
  105. }