You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AudioOutputPreview.js 2.8KB

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