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

AudioOutputPreview.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* @flow */
  2. import React, { Component } from 'react';
  3. import { translate } from '../../base/i18n';
  4. import { Audio } from '../../base/media';
  5. const TEST_SOUND_PATH = 'sounds/ring.wav';
  6. /**
  7. * The type of the React {@code Component} props of {@link AudioOutputPreview}.
  8. */
  9. type Props = {
  10. /**
  11. * The device id of the audio output device to use.
  12. */
  13. deviceId: string,
  14. /**
  15. * Invoked to obtain translated strings.
  16. */
  17. t: Function
  18. };
  19. /**
  20. * React component for playing a test sound through a specified audio device.
  21. *
  22. * @extends Component
  23. */
  24. class AudioOutputPreview extends Component<Props> {
  25. _audioElement: ?Object;
  26. /**
  27. * Initializes a new AudioOutputPreview instance.
  28. *
  29. * @param {Object} props - The read-only React Component props with which
  30. * the new instance is to be initialized.
  31. */
  32. constructor(props: Props) {
  33. super(props);
  34. this._audioElement = null;
  35. this._onClick = this._onClick.bind(this);
  36. this._setAudioElement = this._setAudioElement.bind(this);
  37. }
  38. /**
  39. * Sets the target output device on the component's audio element after
  40. * initial render.
  41. *
  42. * @inheritdoc
  43. * @returns {void}
  44. */
  45. componentDidMount() {
  46. this._setAudioSink();
  47. }
  48. /**
  49. * Updates the audio element when the target output device changes and the
  50. * audio element has re-rendered.
  51. *
  52. * @inheritdoc
  53. * @returns {void}
  54. */
  55. componentDidUpdate() {
  56. this._setAudioSink();
  57. }
  58. /**
  59. * Implements React's {@link Component#render()}.
  60. *
  61. * @inheritdoc
  62. * @returns {ReactElement}
  63. */
  64. render() {
  65. return (
  66. <div className = 'audio-output-preview'>
  67. <a onClick = { this._onClick }>
  68. { this.props.t('deviceSelection.testAudio') }
  69. </a>
  70. <Audio
  71. setRef = { this._setAudioElement }
  72. src = { TEST_SOUND_PATH } />
  73. </div>
  74. );
  75. }
  76. _onClick: () => void;
  77. /**
  78. * Plays a test sound.
  79. *
  80. * @private
  81. * @returns {void}
  82. */
  83. _onClick() {
  84. this._audioElement
  85. && this._audioElement.play();
  86. }
  87. _setAudioElement: (Object) => void;
  88. /**
  89. * Sets the instance variable for the component's audio element so it can be
  90. * accessed directly.
  91. *
  92. * @param {Object} element - The DOM element for the component's audio.
  93. * @private
  94. * @returns {void}
  95. */
  96. _setAudioElement(element: Object) {
  97. this._audioElement = element;
  98. }
  99. /**
  100. * Updates the target output device for playing the test sound.
  101. *
  102. * @private
  103. * @returns {void}
  104. */
  105. _setAudioSink() {
  106. this._audioElement
  107. && this._audioElement.setSinkId(this.props.deviceId);
  108. }
  109. }
  110. export default translate(AudioOutputPreview);