You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
925 B

  1. const createAudio = sources => {
  2. const audio = new Audio();
  3. sources.forEach(({ type, src }) => {
  4. const source = document.createElement('source');
  5. source.type = type;
  6. source.src = src;
  7. audio.appendChild(source);
  8. });
  9. return audio;
  10. };
  11. const play = audio => {
  12. if (!audio.paused) {
  13. audio.pause();
  14. if (typeof audio.fastSeek === 'function') {
  15. audio.fastSeek(0);
  16. } else {
  17. audio.currentTime = 0;
  18. }
  19. }
  20. audio.play();
  21. };
  22. export default function soundsMiddleware() {
  23. const soundCache = {
  24. boop: createAudio([
  25. {
  26. src: '/sounds/boop.ogg',
  27. type: 'audio/ogg',
  28. },
  29. {
  30. src: '/sounds/boop.mp3',
  31. type: 'audio/mpeg',
  32. },
  33. ]),
  34. };
  35. return () => next => action => {
  36. if (action.meta && action.meta.sound && soundCache[action.meta.sound]) {
  37. play(soundCache[action.meta.sound]);
  38. }
  39. return next(action);
  40. };
  41. };