Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SpeakerEntry.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // @flow
  2. import React, { Component } from 'react';
  3. import logger from '../../../logger';
  4. import AudioSettingsEntry from './AudioSettingsEntry';
  5. import TestButton from './TestButton';
  6. const TEST_SOUND_PATH = 'sounds/ring.wav';
  7. /**
  8. * The type of the React {@code Component} props of {@link SpeakerEntry}.
  9. */
  10. type Props = {
  11. /**
  12. * The text label for the entry.
  13. */
  14. children: React$Node,
  15. /**
  16. * Flag controlling the selection state of the entry.
  17. */
  18. isSelected: boolean,
  19. /**
  20. * Flag controlling the selection state of the entry.
  21. */
  22. index: number,
  23. /**
  24. * Flag controlling the selection state of the entry.
  25. */
  26. length: number,
  27. /**
  28. * The deviceId of the speaker.
  29. */
  30. deviceId: string,
  31. /**
  32. * Click handler for the component.
  33. */
  34. onClick: Function,
  35. listHeaderId: string
  36. };
  37. /**
  38. * Implements a React {@link Component} which displays an audio
  39. * output settings entry. The user can click and play a test sound.
  40. *
  41. * @extends Component
  42. */
  43. export default class SpeakerEntry extends Component<Props> {
  44. /**
  45. * A React ref to the HTML element containing the {@code audio} instance.
  46. */
  47. audioRef: Object;
  48. /**
  49. * Initializes a new {@code SpeakerEntry} instance.
  50. *
  51. * @param {Object} props - The read-only properties with which the new
  52. * instance is to be initialized.
  53. */
  54. constructor(props: Props) {
  55. super(props);
  56. this.audioRef = React.createRef();
  57. this._onTestButtonClick = this._onTestButtonClick.bind(this);
  58. this._onClick = this._onClick.bind(this);
  59. this._onKeyPress = this._onKeyPress.bind(this);
  60. }
  61. _onClick: () => void;
  62. /**
  63. * Click handler for the entry.
  64. *
  65. * @returns {void}
  66. */
  67. _onClick() {
  68. this.props.onClick(this.props.deviceId);
  69. }
  70. _onKeyPress: () => void;
  71. /**
  72. * Key pressed handler for the entry.
  73. *
  74. * @param {Object} e - The event.
  75. * @private
  76. *
  77. * @returns {void}
  78. */
  79. _onKeyPress(e) {
  80. if (e.key === ' ') {
  81. e.preventDefault();
  82. this.props.onClick(this.props.deviceId);
  83. }
  84. }
  85. _onTestButtonClick: Object => void;
  86. /**
  87. * Click handler for Test button.
  88. * Sets the current audio output id and plays a sound.
  89. *
  90. * @param {Object} e - The sythetic event.
  91. * @returns {void}
  92. */
  93. async _onTestButtonClick(e) {
  94. e.stopPropagation();
  95. try {
  96. await this.audioRef.current.setSinkId(this.props.deviceId);
  97. this.audioRef.current.play();
  98. } catch (err) {
  99. logger.log('Could not set sink id', err);
  100. }
  101. }
  102. /**
  103. * Implements React's {@link Component#render}.
  104. *
  105. * @inheritdoc
  106. */
  107. render() {
  108. const { children, isSelected, index, deviceId, length, listHeaderId } = this.props;
  109. const deviceTextId: string = `choose_speaker${deviceId}`;
  110. const labelledby: string = `${listHeaderId} ${deviceTextId} `;
  111. return (
  112. <li
  113. aria-checked = { isSelected }
  114. aria-labelledby = { labelledby }
  115. aria-posinset = { index }
  116. aria-setsize = { length }
  117. className = 'audio-preview-speaker'
  118. onClick = { this._onClick }
  119. onKeyPress = { this._onKeyPress }
  120. role = 'radio'
  121. tabIndex = { 0 }>
  122. <AudioSettingsEntry
  123. isSelected = { isSelected }
  124. key = { deviceId }
  125. labelId = { deviceTextId }>
  126. {children}
  127. </AudioSettingsEntry>
  128. <TestButton
  129. onClick = { this._onTestButtonClick }
  130. onKeyPress = { this._onTestButtonClick } />
  131. <audio
  132. preload = 'auto'
  133. ref = { this.audioRef }
  134. src = { TEST_SOUND_PATH } />
  135. </li>
  136. );
  137. }
  138. }