- A spectrogram drawn in the browser from the sound being played: the last seconds, time running to the right and ending now, low notes at the bottom, brighter where there is more energy. Small in the player bar, large in the full-screen player. It works the same for a friend's track, since its audio comes through this server. - It is drawn only while something plays and the page is in view, and with reduced motion it is redrawn four times a second; where the browser has no Web Audio it stays empty and nothing else changes. - The play button is round, in the colour of this server and in the friend colour for a friend's track. - The bar and the full-screen player show the name of the friend whose server a track comes from, and nothing for this server's own music.
58 lines
2.2 KiB
TypeScript
58 lines
2.2 KiB
TypeScript
import { useEffect } from 'react'
|
|
import { sendListen } from '@/api/listens'
|
|
import { uuid } from '@/lib/uuid'
|
|
import { createEngine } from './engine'
|
|
import { attachAnalyser } from './analyser'
|
|
import { createListenReporter } from './listens'
|
|
import { trackKey } from './media'
|
|
import { setMediaSession } from './mediaSession'
|
|
import { currentTrack, usePlayer } from './store'
|
|
|
|
/**
|
|
* Mounts the audio engine once for the whole app. It also reports every play to the server, and keeps
|
|
* the media keys and the lock screen in step.
|
|
*/
|
|
export function usePlayerEngine(): void {
|
|
useEffect(() => {
|
|
const audio = new Audio()
|
|
// The spectrogram listens to the sound: the analyser is connected when something first plays.
|
|
const onPlay = () => attachAnalyser(audio)
|
|
audio.addEventListener('play', onPlay)
|
|
const reporter = createListenReporter({
|
|
send: sendListen,
|
|
uuid,
|
|
now: () => new Date(),
|
|
})
|
|
const engine = createEngine(audio, {
|
|
onStart: reporter.start,
|
|
onPlayed: reporter.addPlayed,
|
|
onFinish: reporter.finish,
|
|
})
|
|
|
|
// Only closing the page ends a play early. A hidden tab keeps playing, so it is not an end.
|
|
const onPageHide = () => reporter.flushOnHide()
|
|
window.addEventListener('pagehide', onPageHide)
|
|
|
|
const keys = {
|
|
play: () => !usePlayer.getState().playing && usePlayer.getState().togglePlay(),
|
|
pause: () => usePlayer.getState().playing && usePlayer.getState().togglePlay(),
|
|
next: () => usePlayer.getState().next(),
|
|
prev: () => usePlayer.getState().prev(),
|
|
}
|
|
const stopMediaSession = usePlayer.subscribe((state, previous) => {
|
|
const track = currentTrack(state)
|
|
const before = currentTrack(previous)
|
|
if ((track ? trackKey(track) : null) !== (before ? trackKey(before) : null)) setMediaSession(track, keys)
|
|
})
|
|
|
|
return () => {
|
|
window.removeEventListener('pagehide', onPageHide)
|
|
stopMediaSession()
|
|
engine.destroy()
|
|
audio.removeEventListener('play', onPlay)
|
|
audio.pause()
|
|
// The app is closing (signed out): the next person to sign in on this page must not inherit the queue.
|
|
usePlayer.getState().playTracks([])
|
|
}
|
|
}, [])
|
|
}
|