您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AbstractAudio.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import React, { Component } from 'react';
  2. /**
  3. * The React {@link Component} which is similar to Web's
  4. * {@code HTMLAudioElement}.
  5. */
  6. export default class AbstractAudio extends Component {
  7. /**
  8. * The (reference to the) {@link ReactElement} which actually implements
  9. * this {@code AbstractAudio}.
  10. */
  11. _ref: ?Object
  12. /**
  13. * {@code AbstractAudio} component's property types.
  14. *
  15. * @static
  16. */
  17. static propTypes = {
  18. /**
  19. * The URL of a media resource to use in the element.
  20. *
  21. * @type {string}
  22. */
  23. src: React.PropTypes.string,
  24. stream: React.PropTypes.object
  25. };
  26. /**
  27. * Initializes a new {@code AbstractAudio} instance.
  28. *
  29. * @param {Object} props - The read-only properties with which the new
  30. * instance is to be initialized.
  31. */
  32. constructor(props) {
  33. super(props);
  34. // Bind event handlers so they are only bound once for every instance.
  35. this._setRef = this._setRef.bind(this);
  36. }
  37. /**
  38. * Attempts to pause the playback of the media.
  39. *
  40. * @public
  41. * @returns {void}
  42. */
  43. pause() {
  44. this._ref && typeof this._ref.pause === 'function' && this._ref.pause();
  45. }
  46. /**
  47. * Attempts to being the playback of the media.
  48. *
  49. * @public
  50. * @returns {void}
  51. */
  52. play() {
  53. this._ref && typeof this._ref.play === 'function' && this._ref.play();
  54. }
  55. /**
  56. * Renders this {@code AbstractAudio} as a React {@link Component} of a
  57. * specific type.
  58. *
  59. * @param {string|ReactClass} type - The type of the React {@code Component}
  60. * which is to be rendered.
  61. * @param {Object|undefined} props - The read-only React {@code Component}
  62. * properties, if any, to render. If {@code undefined}, the props of this
  63. * instance will be rendered.
  64. * @protected
  65. * @returns {ReactElement}
  66. */
  67. _render(type, props) {
  68. const {
  69. children,
  70. /* eslint-disable no-unused-vars */
  71. // The following properties are consumed by React itself so they are
  72. // to not be propagated.
  73. ref,
  74. /* eslint-enable no-unused-vars */
  75. ...filteredProps
  76. } = props || this.props;
  77. return (
  78. React.createElement(
  79. type,
  80. {
  81. ...filteredProps,
  82. ref: this._setRef
  83. },
  84. children));
  85. }
  86. /**
  87. * Set the (reference to the) {@link ReactElement} which actually implements
  88. * this {@code AbstractAudio}.
  89. *
  90. * @param {Object} ref - The (reference to the) {@code ReactElement} which
  91. * actually implements this {@code AbstractAudio}.
  92. * @private
  93. * @returns {void}
  94. */
  95. _setRef(ref) {
  96. this._ref = ref;
  97. }
  98. }