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

AudioOutputPreview.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* @flow */
  2. import React, { Component } from 'react';
  3. import { translate } from '../../base/i18n/functions';
  4. import Audio from '../../base/media/components/Audio';
  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._audioElementReady = this._audioElementReady.bind(this);
  36. this._onClick = this._onClick.bind(this);
  37. }
  38. /**
  39. * Updates the audio element when the target output device changes and the
  40. * audio element has re-rendered.
  41. *
  42. * @inheritdoc
  43. * @returns {void}
  44. */
  45. componentDidUpdate() {
  46. this._setAudioSink();
  47. }
  48. /**
  49. * Implements React's {@link Component#render()}.
  50. *
  51. * @inheritdoc
  52. * @returns {ReactElement}
  53. */
  54. render() {
  55. return (
  56. <div className = 'audio-output-preview'>
  57. <a onClick = { this._onClick }>
  58. { this.props.t('deviceSelection.testAudio') }
  59. </a>
  60. <Audio
  61. setRef = { this._audioElementReady }
  62. src = { TEST_SOUND_PATH } />
  63. </div>
  64. );
  65. }
  66. _audioElementReady: (Object) => void;
  67. /**
  68. * Sets the instance variable for the component's audio element so it can be
  69. * accessed directly.
  70. *
  71. * @param {Object} element - The DOM element for the component's audio.
  72. * @private
  73. * @returns {void}
  74. */
  75. _audioElementReady(element: Object) {
  76. this._audioElement = element;
  77. this._setAudioSink();
  78. }
  79. _onClick: () => void;
  80. /**
  81. * Plays a test sound.
  82. *
  83. * @private
  84. * @returns {void}
  85. */
  86. _onClick() {
  87. this._audioElement
  88. && this._audioElement.play();
  89. }
  90. /**
  91. * Updates the target output device for playing the test sound.
  92. *
  93. * @private
  94. * @returns {void}
  95. */
  96. _setAudioSink() {
  97. this._audioElement
  98. && this.props.deviceId
  99. && this._audioElement.setSinkId(this.props.deviceId);
  100. }
  101. }
  102. export default translate(AudioOutputPreview);