Preload next song
This commit is contained in:
parent
ad45ab4a04
commit
b1e7760996
|
@ -33,6 +33,7 @@ const Player = () => {
|
||||||
const dispatch = useDispatch()
|
const dispatch = useDispatch()
|
||||||
const [startTime, setStartTime] = useState(null)
|
const [startTime, setStartTime] = useState(null)
|
||||||
const [scrobbled, setScrobbled] = useState(false)
|
const [scrobbled, setScrobbled] = useState(false)
|
||||||
|
const [preloaded, setPreload] = useState(false)
|
||||||
const [audioInstance, setAudioInstance] = useState(null)
|
const [audioInstance, setAudioInstance] = useState(null)
|
||||||
const isDesktop = useMediaQuery('(min-width:810px)')
|
const isDesktop = useMediaQuery('(min-width:810px)')
|
||||||
const { authenticated } = useAuthState()
|
const { authenticated } = useAuthState()
|
||||||
|
@ -93,6 +94,13 @@ const Player = () => {
|
||||||
[dispatch]
|
[dispatch]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const nextSong = useCallback(() => {
|
||||||
|
const idx = playerState.queue.findIndex(
|
||||||
|
(item) => item.uuid === playerState.current.uuid
|
||||||
|
)
|
||||||
|
return idx !== null ? playerState.queue[idx + 1] : null
|
||||||
|
}, [playerState])
|
||||||
|
|
||||||
const onAudioProgress = useCallback(
|
const onAudioProgress = useCallback(
|
||||||
(info) => {
|
(info) => {
|
||||||
if (info.ended) {
|
if (info.ended) {
|
||||||
|
@ -104,12 +112,22 @@ const Player = () => {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!preloaded) {
|
||||||
|
const next = nextSong()
|
||||||
|
if (next != null) {
|
||||||
|
const audio = new Audio()
|
||||||
|
audio.src = next.musicSrc
|
||||||
|
}
|
||||||
|
setPreload(true)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if (!scrobbled) {
|
if (!scrobbled) {
|
||||||
info.trackId && subsonic.scrobble(info.trackId, startTime)
|
info.trackId && subsonic.scrobble(info.trackId, startTime)
|
||||||
setScrobbled(true)
|
setScrobbled(true)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[startTime, scrobbled]
|
[startTime, scrobbled, nextSong, preloaded]
|
||||||
)
|
)
|
||||||
|
|
||||||
const onAudioVolumeChange = useCallback(
|
const onAudioVolumeChange = useCallback(
|
||||||
|
@ -126,6 +144,7 @@ const Player = () => {
|
||||||
const song = info.song
|
const song = info.song
|
||||||
document.title = `${song.title} - ${song.artist} - Navidrome`
|
document.title = `${song.title} - ${song.artist} - Navidrome`
|
||||||
subsonic.nowPlaying(info.trackId)
|
subsonic.nowPlaying(info.trackId)
|
||||||
|
setPreload(false)
|
||||||
setScrobbled(false)
|
setScrobbled(false)
|
||||||
if (config.gaTrackingId) {
|
if (config.gaTrackingId) {
|
||||||
ReactGA.event({
|
ReactGA.event({
|
||||||
|
|
Loading…
Reference in New Issue