123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- import {store} from "./store";
- import {actionPromise} from "./promiseReducer";
- import {gql} from "../actions";
- export const playerReducer = (
- state = {},
- {type,track,isPlaying=false,isPaused,duration,
- playlist=[],playlistIndex,currentTime=0,volume=1,}) => {
- if (type === "TRACK_PLAY") {
- return {
- ...state,
- isPlaying,
- isPaused: !isPlaying,
- };
- }
- if (type === "TRACK_STOP") {
- return {
- ...state,
- isPaused,
- isPlaying: !isPaused,
- };
- }
- if (type === "TRACK_VOLUME") {
- return {
- ...state,
- volume,
- };
- }
- if (type === "TRACK_DURATION") {
- return {
- ...state,
- duration,
- };
- }
- if (type === "TRACK_CURRENT_TIME") {
- return {
- ...state,
- currentTime,
- };
- }
- if (type === "SET_PLAYLIST") {
- return {
- ...state,
- playlist,
- };
- }
- return state;
- };
- const audio = new Audio()
- export const actionTrackPlay = (audioSrc) => {
- audio.src = audioSrc
- audio.load()
- audio.ondurationchange = function(){actionSetDuration()}
- audio.ontimeupdate = function(){actionTrackCurrentTime()}
- audio.play()
- store.dispatch({type:"TRACK_PLAY",isPlaying:true,isPaused:false})
- store.dispatch(actionSetPlaylist())
- }
- export const actionTrackStop = () => {
- audio.pause()
- store.dispatch({type:"TRACK_STOP",isPlaying:false,isPaused:true})
- }
- export const actionTrackVolume = (e) => {
- audio.volume = e
- store.dispatch({type:"TRACK_VOLUME", volume:e})
- }
- export const actionSetDuration = () => {
- let e = audio.duration
- store.dispatch({type:"TRACK_DURATION",duration:e})
- }
- export const actionTrackCurrentTime = () => {
- let e = audio.currentTime
- store.dispatch({type:"TRACK_CURRENT_TIME", currentTime:e})
- }
- export const actionSetPlaylist = (tracks) => {
- console.log(tracks,'lol')
- store.dispatch({type:"SET_PLAYLIST",playlist:tracks})
- }
- // const actionPlaylistByIdForPlayer = (match) =>
- // actionPromise('playlistByIdForPlayer', gql(`query playlistByIdForPlayer($q: String){
- // PlaylistFindOne(query: $q){
- // _id name owner {login} tracks {_id url originalFileName}
- // }
- // }`, { q: JSON.stringify([{ _id: match.params._id}]) }))
- //
- //
- //
- // export const actionFullSetPlaylist = (_id) =>
- // async function i(dispatch) {
- // let playlist = await dispatch(actionPlaylistByIdForPlayer(_id));
- // if (playlist) {
- // dispatch(actionSetPlaylist(playlist));
- // }
- // }
|