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

AudioOutputPreview.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import PropTypes from 'prop-types';
  2. import React, { Component } from 'react';
  3. import { translate } from '../../base/i18n';
  4. const TEST_SOUND_PATH = 'sounds/ring.wav';
  5. /**
  6. * React component for playing a test sound through a specified audio device.
  7. *
  8. * @extends Component
  9. */
  10. class AudioOutputPreview extends Component {
  11. /**
  12. * AudioOutputPreview component's property types.
  13. *
  14. * @static
  15. */
  16. static propTypes = {
  17. /**
  18. * The device id of the audio output device to use.
  19. */
  20. deviceId: PropTypes.string,
  21. /**
  22. * Invoked to obtain translated strings.
  23. */
  24. t: PropTypes.func
  25. };
  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) {
  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. preload = 'auto'
  72. ref = { this._setAudioElement }
  73. src = { TEST_SOUND_PATH } />
  74. </div>
  75. );
  76. }
  77. /**
  78. * Plays a test sound.
  79. *
  80. * @private
  81. * @returns {void}
  82. */
  83. _onClick() {
  84. this._audioElement
  85. && this._audioElement.play();
  86. }
  87. /**
  88. * Sets the instance variable for the component's audio element so it can be
  89. * accessed directly.
  90. *
  91. * @param {Object} element - The DOM element for the component's audio.
  92. * @private
  93. * @returns {void}
  94. */
  95. _setAudioElement(element) {
  96. this._audioElement = element;
  97. }
  98. /**
  99. * Updates the target output device for playing the test sound.
  100. *
  101. * @private
  102. * @returns {void}
  103. */
  104. _setAudioSink() {
  105. this._audioElement
  106. && this._audioElement.setSinkId(this.props.deviceId);
  107. }
  108. }
  109. export default translate(AudioOutputPreview);