Files
jitsi-meet/react/features/base/sounds/middleware.js
paweldomas 60e03e3dec feat: add join/leave sounds on mobile
Adds base/sounds feature which allows other features to register a sound
source under specified id. A new SoundsCollection component will then
render corresponding HTMLAudioElement for each such sound. Once "setRef"
callback is called by the HTMLAudioElement, this element will be added
to the Redux store. When that happens sound can be played through the
new 'playSound' action which will call play() method on the stored
HTMLAudioElement instance.
2018-03-13 16:57:28 -05:00

47 lines
1.1 KiB
JavaScript

// @flow
import { MiddlewareRegistry } from '../redux';
import { PLAY_SOUND } from './actionTypes';
const logger = require('jitsi-meet-logger').getLogger(__filename);
/**
* Implements the entry point of the middleware of the feature base/media.
*
* @param {Store} store - The redux store.
* @returns {Function}
*/
MiddlewareRegistry.register(store => next => action => {
switch (action.type) {
case PLAY_SOUND:
_playSound(store, action.soundId);
break;
}
return next(action);
});
/**
* Plays sound from audio element registered in the Redux store.
*
* @param {Store} store - The Redux store instance.
* @param {string} soundId - Audio element identifier.
* @private
* @returns {void}
*/
function _playSound({ getState }, soundId) {
const sounds = getState()['features/base/sounds'];
const sound = sounds.get(soundId);
if (sound) {
if (sound.audioElement) {
sound.audioElement.play();
} else {
logger.warn(`PLAY_SOUND: sound not loaded yet for id: ${soundId}`);
}
} else {
logger.warn(`PLAY_SOUND: no sound found for id: ${soundId}`);
}
}