playerReducer.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //import { store } from '../store/store';
  2. import { audio } from '../components/playlistById';
  3. export const playerReducer = function(state = {}, {type, duration, track, playlist, playlistIndex, currentTime, volume}) {
  4. if (!state) {
  5. return {};
  6. }
  7. if (type === 'PLAY'){
  8. return {
  9. ...state,
  10. isPlaying: true,
  11. isStopped: false
  12. }
  13. } if (type === "PAUSE"){
  14. return {
  15. ...state,
  16. isStopped: true,
  17. isPlaying: false
  18. }
  19. } if (type === 'GET_DURATION') {
  20. return {
  21. ...state,
  22. duration
  23. }
  24. } if (type === 'SET_CURRENT_TIME') {
  25. return {
  26. ...state,
  27. currentTime
  28. }
  29. } if (type === 'SET_VOLUME') {
  30. return {
  31. ...state,
  32. volume
  33. }
  34. } if (type === 'SET_TRACK' || type === 'SET_NEW_TRACK' || type === "SET_PREV_TRACK") {
  35. return {
  36. ...state,
  37. track,
  38. isPlaying: true,
  39. isStopped: false
  40. }
  41. } if (type === 'SET_PLAYLIST') {
  42. return {
  43. ...state,
  44. playlist,
  45. playlistIndex
  46. }
  47. }
  48. return state;
  49. }
  50. const actionPlay = () => ({type:'PLAY'})
  51. export const actionFullPlay = () =>
  52. dispatch => {
  53. audio.play();
  54. dispatch(actionPlay());
  55. dispatch(actionFullGetDuration(audio.duration))
  56. }
  57. const actionPause = () => ({type:'PAUSE'})
  58. export const actionFullPause = () =>
  59. dispatch => {
  60. audio.pause();
  61. dispatch(actionPause());
  62. }
  63. const actionSetVolume = (volume) => ({type:'SET_VOLUME', volume})
  64. export const actionFullSetVolume = (volume) =>
  65. dispatch => {
  66. audio.volume = volume / 100;
  67. dispatch(actionSetVolume(volume));
  68. }
  69. const actionSetTrack = (track) => ({type:'SET_TRACK', track})
  70. export const actionFullSetTrack = (track) =>
  71. dispatch => {
  72. //audio.src = `http://player-api/storage/tracks/${track.file}`;
  73. dispatch(actionSetTrack(track));
  74. dispatch(actionFullPlay());
  75. }
  76. const actionGetDuration = (duration) => ({type:'GET_DURATION', duration})
  77. export const actionFullGetDuration = (duration) =>
  78. dispatch => {
  79. dispatch(actionGetDuration(duration));
  80. }
  81. const actionSetCurrentTime = (currentTime) => ({type:'SET_CURRENT_TIME', currentTime})
  82. export const actionFullSetCurrentTime = (currentTime) =>
  83. dispatch => {
  84. dispatch(actionSetCurrentTime(currentTime));
  85. }
  86. const actionSetPlaylist = (playlist) => ({type:'SET_PLAYLIST', playlist})
  87. export const actionFullSetPlaylist = (playlist) =>
  88. dispatch => {
  89. dispatch(actionSetPlaylist(playlist));
  90. }