2018-10-30 05:02:23 +00:00
|
|
|
/* @flow */
|
|
|
|
|
2017-03-29 17:43:30 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
|
|
|
|
import { translate } from '../../base/i18n';
|
2018-01-24 17:39:18 +00:00
|
|
|
import { Audio } from '../../base/media';
|
2017-03-29 17:43:30 +00:00
|
|
|
|
|
|
|
const TEST_SOUND_PATH = 'sounds/ring.wav';
|
|
|
|
|
2018-10-30 05:02:23 +00:00
|
|
|
/**
|
|
|
|
* The type of the React {@code Component} props of {@link AudioOutputPreview}.
|
|
|
|
*/
|
|
|
|
type Props = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The device id of the audio output device to use.
|
|
|
|
*/
|
|
|
|
deviceId: string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoked to obtain translated strings.
|
|
|
|
*/
|
|
|
|
t: Function
|
|
|
|
};
|
|
|
|
|
2017-03-29 17:43:30 +00:00
|
|
|
/**
|
|
|
|
* React component for playing a test sound through a specified audio device.
|
|
|
|
*
|
|
|
|
* @extends Component
|
|
|
|
*/
|
2018-10-30 05:02:23 +00:00
|
|
|
class AudioOutputPreview extends Component<Props> {
|
|
|
|
_audioElement: ?Object;
|
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.
|
|
|
|
*/
|
2018-10-30 05:02:23 +00:00
|
|
|
constructor(props: Props) {
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 (
|
|
|
|
<div className = 'audio-output-preview'>
|
|
|
|
<a onClick = { this._onClick }>
|
|
|
|
{ this.props.t('deviceSelection.testAudio') }
|
|
|
|
</a>
|
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 } />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-08 17:38:06 +00:00
|
|
|
_audioElementReady: (Object) => void;
|
2018-10-30 05:02:23 +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}
|
|
|
|
*/
|
2019-04-08 17:38:06 +00:00
|
|
|
_audioElementReady(element: Object) {
|
|
|
|
this._audioElement = element;
|
|
|
|
|
|
|
|
this._setAudioSink();
|
2017-03-29 17:43:30 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 17:38:06 +00:00
|
|
|
_onClick: () => void;
|
2018-10-30 05:02:23 +00:00
|
|
|
|
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() {
|
|
|
|
this._audioElement
|
|
|
|
&& this._audioElement.play();
|
2017-03-29 17:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the target output device for playing the test sound.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_setAudioSink() {
|
|
|
|
this._audioElement
|
2019-05-07 08:53:01 +00:00
|
|
|
&& this.props.deviceId
|
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);
|