jiti-meet/react/features/base/media/components/AbstractAudio.js

132 lines
3.3 KiB
JavaScript
Raw Normal View History

// @flow
import { Component } from 'react';
import logger from '../logger';
/**
* Describes audio element interface used in the base/media feature for audio
* playback.
*/
export type AudioElement = {
2019-03-21 16:38:29 +00:00
currentTime: number,
pause: () => void,
play: () => void,
setSinkId?: string => Function,
2018-06-20 13:36:28 +00:00
stop: () => void
};
/**
2021-11-04 21:10:43 +00:00
* {@code AbstractAudio} Component's property types.
*/
type Props = {
/**
* A callback which will be called with {@code AbstractAudio} instance once
* the audio element is loaded.
*/
setRef?: ?AudioElement => void,
/**
* The URL of a media resource to use in the element.
*
* NOTE on react-native sound files are imported through 'require' and then
* passed as the 'src' parameter which means their type will be 'any'.
*
* @type {Object | string}
*/
src: Object | string,
2019-09-13 13:03:40 +00:00
stream?: Object,
loop?: ?boolean
}
/**
* The React {@link Component} which is similar to Web's
* {@code HTMLAudioElement}.
*/
export default class AbstractAudio extends Component<Props> {
/**
* The {@link AudioElement} instance which implements the audio playback
* functionality.
*/
_audioElementImpl: ?AudioElement;
/**
* Initializes a new {@code AbstractAudio} instance.
*
* @param {Props} props - The read-only properties with which the new
* instance is to be initialized.
*/
constructor(props: Props) {
super(props);
// Bind event handlers so they are only bound once per instance.
this.setAudioElementImpl = this.setAudioElementImpl.bind(this);
}
/**
* Attempts to pause the playback of the media.
*
* @public
* @returns {void}
*/
pause(): void {
this._audioElementImpl && this._audioElementImpl.pause();
}
/**
2018-06-20 13:36:28 +00:00
* Attempts to begin the playback of the media.
*
* @public
* @returns {void}
*/
play(): void {
this._audioElementImpl && this._audioElementImpl.play();
}
setAudioElementImpl: ?AudioElement => void;
/**
* Set the (reference to the) {@link AudioElement} object which implements
* the audio playback functionality.
*
* @param {AudioElement} element - The {@link AudioElement} instance
* which implements the audio playback functionality.
* @protected
* @returns {void}
*/
setAudioElementImpl(element: ?AudioElement): void {
this._audioElementImpl = element;
// setRef
const { setRef } = this.props;
// $FlowFixMe
typeof setRef === 'function' && setRef(element ? this : null);
}
/**
* Sets the sink ID (output device ID) on the underlying audio element.
* NOTE: Currently, implemented only on Web.
*
* @param {string} sinkId - The sink ID (output device ID).
* @returns {void}
*/
setSinkId(sinkId: string): void {
this._audioElementImpl
&& typeof this._audioElementImpl.setSinkId === 'function'
&& this._audioElementImpl.setSinkId(sinkId)
.catch(error => logger.error('Error setting sink', error));
}
2018-06-20 13:36:28 +00:00
/**
* Attempts to stop the playback of the media.
*
* @public
* @returns {void}
*/
stop(): void {
this._audioElementImpl && this._audioElementImpl.stop();
}
}