Skip to content

Commit

Permalink
Preload next song
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Oct 1, 2021
1 parent ad45ab4 commit b1e7760
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion ui/src/audioplayer/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const Player = () => {
const dispatch = useDispatch()
const [startTime, setStartTime] = useState(null)
const [scrobbled, setScrobbled] = useState(false)
const [preloaded, setPreload] = useState(false)
const [audioInstance, setAudioInstance] = useState(null)
const isDesktop = useMediaQuery('(min-width:810px)')
const { authenticated } = useAuthState()
Expand Down Expand Up @@ -93,6 +94,13 @@ const Player = () => {
[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(
(info) => {
if (info.ended) {
Expand All @@ -104,12 +112,22 @@ const Player = () => {
return
}

if (!preloaded) {
const next = nextSong()
if (next != null) {
const audio = new Audio()
audio.src = next.musicSrc
}
setPreload(true)
return
}

if (!scrobbled) {
info.trackId && subsonic.scrobble(info.trackId, startTime)
setScrobbled(true)
}
},
[startTime, scrobbled]
[startTime, scrobbled, nextSong, preloaded]
)

const onAudioVolumeChange = useCallback(
Expand All @@ -126,6 +144,7 @@ const Player = () => {
const song = info.song
document.title = `${song.title} - ${song.artist} - Navidrome`
subsonic.nowPlaying(info.trackId)
setPreload(false)
setScrobbled(false)
if (config.gaTrackingId) {
ReactGA.event({
Expand Down

0 comments on commit b1e7760

Please sign in to comment.