2017-05-10 17:58:54 +03:00
|
|
|
const createAudio = sources => {
|
|
|
|
const audio = new Audio();
|
|
|
|
sources.forEach(({ type, src }) => {
|
|
|
|
const source = document.createElement('source');
|
|
|
|
source.type = type;
|
|
|
|
source.src = src;
|
|
|
|
audio.appendChild(source);
|
|
|
|
});
|
|
|
|
return audio;
|
2017-05-20 18:31:47 +03:00
|
|
|
};
|
2017-05-10 17:58:54 +03:00
|
|
|
|
2017-03-13 18:12:30 +02:00
|
|
|
const play = audio => {
|
|
|
|
if (!audio.paused) {
|
|
|
|
audio.pause();
|
2017-10-18 18:13:51 +03:00
|
|
|
if (typeof audio.fastSeek === 'function') {
|
|
|
|
audio.fastSeek(0);
|
|
|
|
} else {
|
2017-12-09 02:25:00 +02:00
|
|
|
audio.currentTime = 0;
|
2017-10-18 18:13:51 +03:00
|
|
|
}
|
2017-03-13 18:12:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
audio.play();
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function soundsMiddleware() {
|
|
|
|
const soundCache = {
|
2017-05-10 17:58:54 +03:00
|
|
|
boop: createAudio([
|
|
|
|
{
|
|
|
|
src: '/sounds/boop.ogg',
|
|
|
|
type: 'audio/ogg',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
src: '/sounds/boop.mp3',
|
2017-05-20 18:31:47 +03:00
|
|
|
type: 'audio/mpeg',
|
2017-05-10 17:58:54 +03:00
|
|
|
},
|
|
|
|
]),
|
2017-03-13 18:12:30 +02:00
|
|
|
};
|
|
|
|
|
2017-06-23 17:05:04 +03:00
|
|
|
return () => next => action => {
|
2017-03-13 18:12:30 +02:00
|
|
|
if (action.meta && action.meta.sound && soundCache[action.meta.sound]) {
|
|
|
|
play(soundCache[action.meta.sound]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
};
|
|
|
|
};
|