feat(web): the audio follows the volume and mute of the player
The audio starts at the remembered level and follows every change, also while a track plays; once the engine is gone it is left alone.
This commit is contained in:
parent
394208de75
commit
edab170d3f
2 changed files with 44 additions and 0 deletions
|
|
@ -12,6 +12,8 @@ class FakeAudio implements AudioLike {
|
|||
currentTime = 0
|
||||
duration = Number.NaN
|
||||
paused = true
|
||||
volume = 1
|
||||
muted = false
|
||||
playCalls = 0
|
||||
private url = ''
|
||||
private handlers = new Map<string, Set<() => void>>()
|
||||
|
|
@ -87,6 +89,8 @@ beforeEach(async () => {
|
|||
seekMs: null,
|
||||
seekId: 0,
|
||||
playId: 0,
|
||||
volume: 1,
|
||||
muted: false,
|
||||
})
|
||||
sessionStore.setState({ status: 'anonymous', user: null, accessToken: null, accessExpiresAt: 0, refreshToken: null })
|
||||
hooks.onStart.mockClear()
|
||||
|
|
@ -102,6 +106,38 @@ afterEach(() => {
|
|||
engine.destroy()
|
||||
})
|
||||
|
||||
test('the audio starts at the volume of the player, muted or not', () => {
|
||||
usePlayer.setState({ volume: 0.3, muted: true })
|
||||
const other = new FakeAudio()
|
||||
const second = createEngine(other, hooks)
|
||||
expect(other.volume).toBe(0.3)
|
||||
expect(other.muted).toBe(true)
|
||||
second.destroy()
|
||||
})
|
||||
|
||||
test('moving the volume changes the audio at once, also while it plays', () => {
|
||||
usePlayer.getState().playTracks([t1])
|
||||
usePlayer.getState().setVolume(0.2)
|
||||
expect(audio.volume).toBe(0.2)
|
||||
expect(audio.muted).toBe(false)
|
||||
})
|
||||
|
||||
test('mute silences the audio and keeps its level, and unmute brings it back', () => {
|
||||
usePlayer.getState().setVolume(0.6)
|
||||
usePlayer.getState().toggleMute()
|
||||
expect(audio.muted).toBe(true)
|
||||
expect(audio.volume).toBe(0.6)
|
||||
usePlayer.getState().toggleMute()
|
||||
expect(audio.muted).toBe(false)
|
||||
expect(audio.volume).toBe(0.6)
|
||||
})
|
||||
|
||||
test('once the engine is gone the audio is left alone', () => {
|
||||
engine.destroy()
|
||||
usePlayer.getState().setVolume(0.1)
|
||||
expect(audio.volume).toBe(1)
|
||||
})
|
||||
|
||||
test('the current track is loaded from its stream URL with the token and played', () => {
|
||||
usePlayer.getState().playTracks([t1, t2])
|
||||
expect(audio.src).toBe('/api/v1/tracks/1/stream?access_token=tok1')
|
||||
|
|
|
|||
|
|
@ -9,6 +9,9 @@ export interface AudioLike {
|
|||
currentTime: number
|
||||
duration: number
|
||||
paused: boolean
|
||||
/** From 0 to 1. */
|
||||
volume: number
|
||||
muted: boolean
|
||||
play(): Promise<void>
|
||||
pause(): void
|
||||
addEventListener(type: AudioEvent, fn: () => void): void
|
||||
|
|
@ -86,6 +89,8 @@ export function createEngine(audio: AudioLike, hooks: EngineHooks = {}): { destr
|
|||
}
|
||||
|
||||
function sync(state: PlayerState, previous: PlayerState) {
|
||||
if (state.volume !== previous.volume) audio.volume = state.volume
|
||||
if (state.muted !== previous.muted) audio.muted = state.muted
|
||||
const track = currentTrack(state)
|
||||
if ((track?.id ?? null) !== loadedId || state.playId !== loadedPlayId) {
|
||||
load(state)
|
||||
|
|
@ -98,6 +103,9 @@ export function createEngine(audio: AudioLike, hooks: EngineHooks = {}): { destr
|
|||
}
|
||||
}
|
||||
|
||||
// The audio starts at the volume the player remembers; after that it follows every change.
|
||||
audio.volume = initial.volume
|
||||
audio.muted = initial.muted
|
||||
const unsubscribeStore = usePlayer.subscribe(sync)
|
||||
sync(initial, { ...initial, queue: [], playId: initial.playId - 1 })
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue