Restore volume level after a refresh

This commit is contained in:
Deluan 2020-06-24 15:33:59 -04:00
parent 04d598819d
commit 6563897692
3 changed files with 24 additions and 4 deletions

View File

@ -5,7 +5,7 @@ import { useAuthState, useDataProvider, useTranslate } from 'react-admin'
import ReactJkMusicPlayer from 'react-jinke-music-player' import ReactJkMusicPlayer from 'react-jinke-music-player'
import 'react-jinke-music-player/assets/index.css' import 'react-jinke-music-player/assets/index.css'
import subsonic from '../subsonic' import subsonic from '../subsonic'
import { scrobble, syncQueue, currentPlaying } from './queue' import { scrobble, syncQueue, currentPlaying, setVolume } from './queue'
import themes from '../themes' import themes from '../themes'
import { makeStyles } from '@material-ui/core/styles' import { makeStyles } from '@material-ui/core/styles'
@ -93,8 +93,9 @@ const Player = () => {
...defaultOptions, ...defaultOptions,
clearPriorAudioLists: queue.clear, clearPriorAudioLists: queue.clear,
audioLists: queue.queue.map((item) => item), audioLists: queue.queue.map((item) => item),
defaultVolume: queue.volume,
} }
}, [queue.clear, queue.queue, defaultOptions]) }, [queue.clear, queue.queue, queue.volume, defaultOptions])
const OnAudioListsChange = useCallback( const OnAudioListsChange = useCallback(
(currentPlayIndex, audioLists) => { (currentPlayIndex, audioLists) => {
@ -121,6 +122,11 @@ const Player = () => {
[dispatch, queue.queue] [dispatch, queue.queue]
) )
const onAudioVolumeChange = useCallback(
(volume) => dispatch(setVolume(volume)),
[dispatch]
)
const OnAudioPlay = useCallback( const OnAudioPlay = useCallback(
(info) => { (info) => {
dispatch(currentPlaying(info)) dispatch(currentPlaying(info))
@ -157,6 +163,7 @@ const Player = () => {
onAudioPlay={OnAudioPlay} onAudioPlay={OnAudioPlay}
onAudioPause={onAudioPause} onAudioPause={onAudioPause}
onAudioEnded={onAudioEnded} onAudioEnded={onAudioEnded}
onAudioVolumeChange={onAudioVolumeChange}
/> />
) )
} }

View File

@ -8,6 +8,7 @@ const PLAYER_CLEAR_QUEUE = 'PLAYER_CLEAR_QUEUE'
const PLAYER_SCROBBLE = 'PLAYER_SCROBBLE' const PLAYER_SCROBBLE = 'PLAYER_SCROBBLE'
const PLAYER_PLAY_TRACKS = 'PLAYER_PLAY_TRACKS' const PLAYER_PLAY_TRACKS = 'PLAYER_PLAY_TRACKS'
const PLAYER_CURRENT = 'PLAYER_CURRENT' const PLAYER_CURRENT = 'PLAYER_CURRENT'
const PLAYER_SET_VOLUME = 'PLAYER_SET_VOLUME'
const mapToAudioLists = (item) => { const mapToAudioLists = (item) => {
// If item comes from a playlist, id is mediaFileId // If item comes from a playlist, id is mediaFileId
@ -99,7 +100,12 @@ const currentPlaying = (audioInfo) => ({
data: audioInfo, data: audioInfo,
}) })
const initialState = { queue: [], clear: true, current: {} } const setVolume = (volume) => ({
type: PLAYER_SET_VOLUME,
data: { volume },
})
const initialState = { queue: [], clear: true, current: {}, volume: 1 }
const playQueueReducer = (previousState = initialState, payload) => { const playQueueReducer = (previousState = initialState, payload) => {
let queue, current let queue, current
@ -107,6 +113,11 @@ const playQueueReducer = (previousState = initialState, payload) => {
switch (type) { switch (type) {
case PLAYER_CLEAR_QUEUE: case PLAYER_CLEAR_QUEUE:
return initialState return initialState
case PLAYER_SET_VOLUME:
return {
...previousState,
volume: data.volume,
}
case PLAYER_CURRENT: case PLAYER_CURRENT:
queue = previousState.queue queue = previousState.queue
current = data.ended current = data.ended
@ -118,6 +129,7 @@ const playQueueReducer = (previousState = initialState, payload) => {
return { return {
...previousState, ...previousState,
current, current,
volume: data.volume,
} }
case PLAYER_ADD_TRACKS: case PLAYER_ADD_TRACKS:
queue = previousState.queue queue = previousState.queue
@ -181,6 +193,7 @@ export {
clearQueue, clearQueue,
scrobble, scrobble,
currentPlaying, currentPlaying,
setVolume,
shuffleTracks, shuffleTracks,
playQueueReducer, playQueueReducer,
} }

View File

@ -48,7 +48,7 @@ export default ({
const state = store.getState() const state = store.getState()
saveState({ saveState({
theme: state.theme, theme: state.theme,
queue: pick(state.queue, ['queue']), queue: pick(state.queue, ['queue', 'volume']),
albumView: state.albumView, albumView: state.albumView,
}) })
}), }),