From bf51c7bcdde0c1c0b6b87f3d41a681bb55a87c7d Mon Sep 17 00:00:00 2001 From: OMGDuke Date: Fri, 7 Apr 2023 23:51:55 +0100 Subject: [PATCH] Fix specific game volume not loading --- package.json | 2 +- src/components/themePlayer/index.tsx | 19 ++++++++++++++++--- src/hooks/useThemeMusic.ts | 1 - 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index f17abc3..0bb3198 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sdh-gamethememusic", - "version": "1.3.0", + "version": "1.3.1", "description": "Play theme songs on your game pages", "scripts": { "build": "shx rm -rf dist && rollup -c", diff --git a/src/components/themePlayer/index.tsx b/src/components/themePlayer/index.tsx index 1ce46b1..85369bd 100644 --- a/src/components/themePlayer/index.tsx +++ b/src/components/themePlayer/index.tsx @@ -1,8 +1,9 @@ import { ServerAPI, useParams } from 'decky-frontend-lib' -import React, { ReactElement, useEffect, useRef } from 'react' +import React, { ReactElement, useEffect, useRef, useState } from 'react' import useThemeMusic from '../../hooks/useThemeMusic' import { useSettings } from '../../context/settingsContext' +import { getCache } from '../../cache/musicCache' export default function ThemePlayer({ serverAPI @@ -13,20 +14,32 @@ export default function ThemePlayer({ const { appid } = useParams<{ appid: string }>() const audioRef = useRef(null) const { audio } = useThemeMusic(serverAPI, parseInt(appid)) + const [volume, setVolume] = useState(settingsState.volume) + + useEffect(() => { + async function getData() { + const cache = await getCache(parseInt(appid)) + if (typeof cache?.volume === 'number' && isFinite(cache.volume)) { + setVolume(cache.volume) + } + } + getData() + }, []) useEffect(() => { if (audio?.audioUrl?.length && audioRef.current) { audioRef.current.src = audio?.audioUrl - audioRef.current.volume = settingsState.volume + audioRef.current.volume = volume audioRef.current.play() } return () => { if (audioRef.current) { audioRef.current.pause() + audioRef.current.volume = 0 audioRef.current.src = '' } } - }, [audio?.audioUrl, audioRef]) + }, [audio?.audioUrl, audioRef?.current, volume]) if (!audio?.audioUrl?.length) return <> diff --git a/src/hooks/useThemeMusic.ts b/src/hooks/useThemeMusic.ts index 5a74d52..9fb2ea4 100644 --- a/src/hooks/useThemeMusic.ts +++ b/src/hooks/useThemeMusic.ts @@ -8,7 +8,6 @@ import { useSettings } from '../context/settingsContext' const useThemeMusic = (serverAPI: ServerAPI, appId: number) => { const { state: settingsState } = useSettings() - console.log('defaultMuted', settingsState.defaultMuted) const [audio, setAudio] = useState<{ videoId: string; audioUrl: string }>({ videoId: '', audioUrl: ''