Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AbstractAudio.ts 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { Component } from 'react';
  2. import logger from '../logger';
  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?: (id: string) => Promise<any>;
  12. stop: () => void;
  13. };
  14. /**
  15. * {@code AbstractAudio} Component's property types.
  16. */
  17. export interface IProps {
  18. loop?: boolean;
  19. /**
  20. * A callback which will be called with {@code AbstractAudio} instance once
  21. * the audio element is loaded.
  22. */
  23. setRef?: (ref?: any) => void;
  24. /**
  25. * The URL of a media resource to use in the element.
  26. *
  27. * NOTE on react-native sound files are imported through 'require' and then
  28. * passed as the 'src' parameter which means their type will be 'any'.
  29. *
  30. * @type {Object | string}
  31. */
  32. src: any | string;
  33. stream?: Object;
  34. }
  35. /**
  36. * The React {@link Component} which is similar to Web's
  37. * {@code HTMLAudioElement}.
  38. */
  39. export default class AbstractAudio extends Component<IProps> {
  40. /**
  41. * The {@link AudioElement} instance which implements the audio playback
  42. * functionality.
  43. */
  44. _audioElementImpl?: AudioElement | null;
  45. /**
  46. * Initializes a new {@code AbstractAudio} instance.
  47. *
  48. * @param {IProps} props - The read-only properties with which the new
  49. * instance is to be initialized.
  50. */
  51. constructor(props: IProps) {
  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() {
  63. this._audioElementImpl?.pause();
  64. }
  65. /**
  66. * Attempts to begin the playback of the media.
  67. *
  68. * @public
  69. * @returns {void}
  70. */
  71. play() {
  72. 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 | null | any) {
  84. this._audioElementImpl = element;
  85. const { setRef } = this.props;
  86. typeof setRef === 'function' && setRef(element ? this : null);
  87. }
  88. /**
  89. * Sets the sink ID (output device ID) on the underlying audio element.
  90. * NOTE: Currently, implemented only on Web.
  91. *
  92. * @param {string} sinkId - The sink ID (output device ID).
  93. * @returns {void}
  94. */
  95. setSinkId(sinkId: string) {
  96. this._audioElementImpl
  97. && typeof this._audioElementImpl.setSinkId === 'function'
  98. && this._audioElementImpl.setSinkId(sinkId)
  99. .catch(error => logger.error('Error setting sink', error));
  100. }
  101. /**
  102. * Attempts to stop the playback of the media.
  103. *
  104. * @public
  105. * @returns {void}
  106. */
  107. stop() {
  108. this._audioElementImpl?.stop();
  109. }
  110. }