2017-03-29 17:43:30 +00:00
|
|
|
import React, { Component } from 'react';
|
2023-03-06 13:14:52 +00:00
|
|
|
import { WithTranslation } from 'react-i18next';
|
2017-03-29 17:43:30 +00:00
|
|
|
|
2019-09-13 13:03:40 +00:00
|
|
|
import { translate } from '../../base/i18n/functions';
|
2023-03-06 13:14:52 +00:00
|
|
|
// eslint-disable-next-line lines-around-comment
|
|
|
|
// @ts-ignore
|
|
|
|
import Audio from '../../base/media/components/Audio.web';
|
|
|
|
import Button from '../../base/ui/components/web/Button';
|
|
|
|
import { BUTTON_TYPES } from '../../base/ui/constants.any';
|
2017-03-29 17:43:30 +00:00
|
|
|
|
2022-11-10 16:06:40 +00:00
|
|
|
const TEST_SOUND_PATH = 'sounds/ring.mp3';
|
2017-03-29 17:43:30 +00:00
|
|
|
|
2018-10-30 05:02:23 +00:00
|
|
|
/**
|
|
|
|
* The type of the React {@code Component} props of {@link AudioOutputPreview}.
|
|
|
|
*/
|
2023-03-06 13:14:52 +00:00
|
|
|
interface IProps extends WithTranslation {
|
2018-10-30 05:02:23 +00:00
|
|
|
|
|
|
|
/**
|
2023-03-06 13:14:52 +00:00
|
|
|
* Button className.
|
2018-10-30 05:02:23 +00:00
|
|
|
*/
|
2023-03-06 13:14:52 +00:00
|
|
|
className?: string;
|
2018-10-30 05:02:23 +00:00
|
|
|
|
|
|
|
/**
|
2023-03-06 13:14:52 +00:00
|
|
|
* The device id of the audio output device to use.
|
2018-10-30 05:02:23 +00:00
|
|
|
*/
|
2023-03-06 13:14:52 +00:00
|
|
|
deviceId: string;
|
|
|
|
}
|
2018-10-30 05:02:23 +00:00
|
|
|
|
2017-03-29 17:43:30 +00:00
|
|
|
/**
|
|
|
|
* React component for playing a test sound through a specified audio device.
|
|
|
|
*
|
2021-11-04 21:10:43 +00:00
|
|
|
* @augments Component
|
2017-03-29 17:43:30 +00:00
|
|
|
*/
|
2023-03-06 13:14:52 +00:00
|
|
|
class AudioOutputPreview extends Component<IProps> {
|
|
|
|
_audioElement: HTMLAudioElement | null;
|
2017-03-29 17:43:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new AudioOutputPreview instance.
|
|
|
|
*
|
|
|
|
* @param {Object} props - The read-only React Component props with which
|
|
|
|
* the new instance is to be initialized.
|
|
|
|
*/
|
2023-03-06 13:14:52 +00:00
|
|
|
constructor(props: IProps) {
|
2017-03-29 17:43:30 +00:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
this._audioElement = null;
|
|
|
|
|
2019-04-08 17:38:06 +00:00
|
|
|
this._audioElementReady = this._audioElementReady.bind(this);
|
2017-03-29 17:43:30 +00:00
|
|
|
this._onClick = this._onClick.bind(this);
|
2021-06-10 12:48:44 +00:00
|
|
|
this._onKeyPress = this._onKeyPress.bind(this);
|
2017-03-29 17:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the audio element when the target output device changes and the
|
|
|
|
* audio element has re-rendered.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
componentDidUpdate() {
|
|
|
|
this._setAudioSink();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#render()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
render() {
|
|
|
|
return (
|
2023-03-06 13:14:52 +00:00
|
|
|
<>
|
|
|
|
<Button
|
|
|
|
accessibilityLabel = { this.props.t('deviceSelection.testAudio') }
|
|
|
|
className = { this.props.className }
|
|
|
|
labelKey = 'deviceSelection.testAudio'
|
2021-06-10 12:48:44 +00:00
|
|
|
onClick = { this._onClick }
|
|
|
|
onKeyPress = { this._onKeyPress }
|
2023-03-06 13:14:52 +00:00
|
|
|
type = { BUTTON_TYPES.SECONDARY } />
|
2018-01-24 17:39:18 +00:00
|
|
|
<Audio
|
2019-04-08 17:38:06 +00:00
|
|
|
setRef = { this._audioElementReady }
|
2017-03-29 17:43:30 +00:00
|
|
|
src = { TEST_SOUND_PATH } />
|
2023-03-06 13:14:52 +00:00
|
|
|
</>
|
2017-03-29 17:43:30 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-08 17:38:06 +00:00
|
|
|
* Sets the instance variable for the component's audio element so it can be
|
|
|
|
* accessed directly.
|
2017-03-29 17:43:30 +00:00
|
|
|
*
|
2019-04-08 17:38:06 +00:00
|
|
|
* @param {Object} element - The DOM element for the component's audio.
|
2017-03-29 17:43:30 +00:00
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2023-03-06 13:14:52 +00:00
|
|
|
_audioElementReady(element: HTMLAudioElement) {
|
2019-04-08 17:38:06 +00:00
|
|
|
this._audioElement = element;
|
|
|
|
|
|
|
|
this._setAudioSink();
|
2017-03-29 17:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-08 17:38:06 +00:00
|
|
|
* Plays a test sound.
|
2017-03-29 17:43:30 +00:00
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2019-04-08 17:38:06 +00:00
|
|
|
_onClick() {
|
2023-03-06 13:14:52 +00:00
|
|
|
this._audioElement?.play();
|
2017-03-29 17:43:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 12:48:44 +00:00
|
|
|
/**
|
|
|
|
* KeyPress handler for accessibility.
|
|
|
|
*
|
|
|
|
* @param {Object} e - The key event to handle.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2023-03-06 13:14:52 +00:00
|
|
|
_onKeyPress(e: React.KeyboardEvent) {
|
2021-06-10 12:48:44 +00:00
|
|
|
if (e.key === ' ' || e.key === 'Enter') {
|
|
|
|
e.preventDefault();
|
|
|
|
this._onClick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 17:43:30 +00:00
|
|
|
/**
|
|
|
|
* Updates the target output device for playing the test sound.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_setAudioSink() {
|
|
|
|
this._audioElement
|
2023-03-06 13:14:52 +00:00
|
|
|
&& this.props.deviceId // @ts-ignore
|
2019-04-08 17:38:06 +00:00
|
|
|
&& this._audioElement.setSinkId(this.props.deviceId);
|
2017-03-29 17:43:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default translate(AudioOutputPreview);
|