Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

AudioOutputPreview.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import PropTypes from 'prop-types';
  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. * React component for playing a test sound through a specified audio device.
  8. *
  9. * @extends Component
  10. */
  11. class AudioOutputPreview extends Component {
  12. /**
  13. * AudioOutputPreview component's property types.
  14. *
  15. * @static
  16. */
  17. static propTypes = {
  18. /**
  19. * The device id of the audio output device to use.
  20. */
  21. deviceId: PropTypes.string,
  22. /**
  23. * Invoked to obtain translated strings.
  24. */
  25. t: PropTypes.func
  26. };
  27. /**
  28. * Initializes a new AudioOutputPreview instance.
  29. *
  30. * @param {Object} props - The read-only React Component props with which
  31. * the new instance is to be initialized.
  32. */
  33. constructor(props) {
  34. super(props);
  35. this._audioElement = null;
  36. this._onClick = this._onClick.bind(this);
  37. this._setAudioElement = this._setAudioElement.bind(this);
  38. }
  39. /**
  40. * Sets the target output device on the component's audio element after
  41. * initial render.
  42. *
  43. * @inheritdoc
  44. * @returns {void}
  45. */
  46. componentDidMount() {
  47. this._setAudioSink();
  48. }
  49. /**
  50. * Updates the audio element when the target output device changes and the
  51. * audio element has re-rendered.
  52. *
  53. * @inheritdoc
  54. * @returns {void}
  55. */
  56. componentDidUpdate() {
  57. this._setAudioSink();
  58. }
  59. /**
  60. * Implements React's {@link Component#render()}.
  61. *
  62. * @inheritdoc
  63. * @returns {ReactElement}
  64. */
  65. render() {
  66. return (
  67. <div className = 'audio-output-preview'>
  68. <a onClick = { this._onClick }>
  69. { this.props.t('deviceSelection.testAudio') }
  70. </a>
  71. <Audio
  72. setRef = { 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);