playerReducer.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import {store} from "./store";
  2. import {actionPromise} from "./promiseReducer";
  3. import {gql} from "../actions";
  4. export const playerReducer = (
  5. state = {},
  6. {type,track,isPlaying=false,isPaused,duration,
  7. playlist=[],playlistIndex,currentTime=0,volume=1,}) => {
  8. if (type === "TRACK_PLAY") {
  9. return {
  10. ...state,
  11. isPlaying,
  12. isPaused: !isPlaying,
  13. };
  14. }
  15. if (type === "TRACK_STOP") {
  16. return {
  17. ...state,
  18. isPaused,
  19. isPlaying: !isPaused,
  20. };
  21. }
  22. if (type === "TRACK_VOLUME") {
  23. return {
  24. ...state,
  25. volume,
  26. };
  27. }
  28. if (type === "TRACK_DURATION") {
  29. return {
  30. ...state,
  31. duration,
  32. };
  33. }
  34. if (type === "TRACK_CURRENT_TIME") {
  35. return {
  36. ...state,
  37. currentTime,
  38. };
  39. }
  40. if (type === "SET_PLAYLIST") {
  41. return {
  42. ...state,
  43. playlist,
  44. };
  45. }
  46. return state;
  47. };
  48. const audio = new Audio()
  49. export const actionTrackPlay = (audioSrc) => {
  50. audio.src = audioSrc
  51. audio.load()
  52. audio.ondurationchange = function(){actionSetDuration()}
  53. audio.ontimeupdate = function(){actionTrackCurrentTime()}
  54. audio.play()
  55. store.dispatch({type:"TRACK_PLAY",isPlaying:true,isPaused:false})
  56. store.dispatch(actionSetPlaylist())
  57. }
  58. export const actionTrackStop = () => {
  59. audio.pause()
  60. store.dispatch({type:"TRACK_STOP",isPlaying:false,isPaused:true})
  61. }
  62. export const actionTrackVolume = (e) => {
  63. audio.volume = e
  64. store.dispatch({type:"TRACK_VOLUME", volume:e})
  65. }
  66. export const actionSetDuration = () => {
  67. let e = audio.duration
  68. store.dispatch({type:"TRACK_DURATION",duration:e})
  69. }
  70. export const actionTrackCurrentTime = () => {
  71. let e = audio.currentTime
  72. store.dispatch({type:"TRACK_CURRENT_TIME", currentTime:e})
  73. }
  74. export const actionSetPlaylist = (tracks) => {
  75. console.log(tracks,'lol')
  76. store.dispatch({type:"SET_PLAYLIST",playlist:tracks})
  77. }
  78. // const actionPlaylistByIdForPlayer = (match) =>
  79. // actionPromise('playlistByIdForPlayer', gql(`query playlistByIdForPlayer($q: String){
  80. // PlaylistFindOne(query: $q){
  81. // _id name owner {login} tracks {_id url originalFileName}
  82. // }
  83. // }`, { q: JSON.stringify([{ _id: match.params._id}]) }))
  84. //
  85. //
  86. //
  87. // export const actionFullSetPlaylist = (_id) =>
  88. // async function i(dispatch) {
  89. // let playlist = await dispatch(actionPlaylistByIdForPlayer(_id));
  90. // if (playlist) {
  91. // dispatch(actionSetPlaylist(playlist));
  92. // }
  93. // }