audio.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import song from "../../../assets/song.mp3"; // delete
  2. import song2 from "../../../assets/little.mp3"; // delete
  3. import store from "../../store";
  4. import types from "../types";
  5. const audio = new Audio(song);
  6. export const togglePlay = (status) => {
  7. status ? audio.play() : audio.pause();
  8. return { type: types.TOGGLE_PLAY, payload: status };
  9. };
  10. export const setDuration = (e) => {
  11. const value = e.target.duration;
  12. store.dispatch({
  13. type: types.SET_DURATION,
  14. payload: value,
  15. });
  16. };
  17. export const setCurrentTime = (value) => {
  18. audio.currentTime = value;
  19. return { type: types.SET_CURRENT_TIME, payload: value };
  20. };
  21. export const setVolume = (value) => {
  22. audio.volume = value;
  23. return { type: types.SET_VOLUME, payload: value };
  24. };
  25. export const toggleRepeat = (status) => {
  26. audio.loop = status;
  27. return { type: types.TOGGLE_REPEAT, payload: status };
  28. };
  29. // audio listeners
  30. const onEnded = () => {
  31. if (!audio.loop) {
  32. //next track if exists(if no toggle_play)
  33. }
  34. };
  35. const onTimeUpdate = (e) => {
  36. store.dispatch({
  37. type: types.SET_CURRENT_TIME,
  38. payload: e.target.currentTime,
  39. });
  40. };
  41. audio.addEventListener("ended", onEnded);
  42. audio.addEventListener("timeupdate", onTimeUpdate);
  43. audio.addEventListener("durationchange", setDuration);