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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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: Function,
  9. play: Function,
  10. setSinkId: ?Function
  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: ?Function,
  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. /**
  54. * Attempts to pause the playback of the media.
  55. *
  56. * @public
  57. * @returns {void}
  58. */
  59. pause() {
  60. this._audioElementImpl && this._audioElementImpl.pause();
  61. }
  62. /**
  63. * Attempts to being the playback of the media.
  64. *
  65. * @public
  66. * @returns {void}
  67. */
  68. play() {
  69. this._audioElementImpl && this._audioElementImpl.play();
  70. }
  71. setAudioElementImpl: (?AudioElement) => void;
  72. /**
  73. * Set the (reference to the) {@link AudioElement} object which implements
  74. * the audio playback functionality.
  75. *
  76. * @param {AudioElement} element - The {@link AudioElement} instance
  77. * which implements the audio playback functionality.
  78. * @protected
  79. * @returns {void}
  80. */
  81. setAudioElementImpl(element: ?AudioElement) {
  82. this._audioElementImpl = element;
  83. // setRef
  84. const { setRef } = this.props;
  85. typeof setRef === 'function' && setRef(element ? this : null);
  86. }
  87. /**
  88. * Sets the sink ID (output device ID) on the underlying audio element.
  89. * NOTE: Currently, implemented only on Web.
  90. *
  91. * @param {string} sinkId - The sink ID (output device ID).
  92. * @returns {void}
  93. */
  94. setSinkId(sinkId: String) {
  95. this._audioElementImpl
  96. && typeof this._audioElementImpl.setSinkId === 'function'
  97. && this._audioElementImpl.setSinkId(sinkId);
  98. }
  99. }