diff --git a/discover/src/PlayerBar.tsx b/discover/src/PlayerBar.tsx
index 0be9a1e..8543744 100644
--- a/discover/src/PlayerBar.tsx
+++ b/discover/src/PlayerBar.tsx
@@ -3,7 +3,7 @@ import { Cover, formatTime } from "./TrackRow";
import * as Icon from "./icons";
export function PlayerBar({ player }: { player: Player }) {
- const { current, status, time, duration } = player;
+ const { current, status, time, duration, volume, muted, videoId } = player;
if (!current) return null;
const label =
status === "loading"
@@ -18,9 +18,21 @@ export function PlayerBar({ player }: { player: Player }) {
{current.title}
{current.artist}
-
- {current.source === "youtube" ? "YouTube" : "Library"}
-
+ {current.source === "youtube" && videoId ? (
+
+ YouTube
+
+ ) : (
+
+ {current.source === "youtube" ? "YouTube" : "Library"}
+
+ )}
@@ -47,8 +59,25 @@ export function PlayerBar({ player }: { player: Player }) {
{label &&
{label}
}
-
- {player.index + 1} / {player.queue.length}
+
+
+
+ player.setVolume(Number(e.target.value))}
+ aria-label="Volume"
+ style={{ ["--progress" as string]: `${(muted ? 0 : volume) * 100}%` }}
+ />
+
+
+ {player.index + 1} / {player.queue.length}
+
);
diff --git a/discover/src/icons.tsx b/discover/src/icons.tsx
index 7370538..c864117 100644
--- a/discover/src/icons.tsx
+++ b/discover/src/icons.tsx
@@ -21,3 +21,5 @@ export const ListMusic = ({ size }: P) => svg(<>
svg(<>>, size);
export const Warning = ({ size }: P) => svg(<>>, size);
export const User = ({ size }: P) => svg(<>>, size);
+export const Volume = ({ size }: P) => svg(<>>, size);
+export const Muted = ({ size }: P) => svg(<>>, size);
diff --git a/discover/src/styles.css b/discover/src/styles.css
index 1defaa3..f40b978 100644
--- a/discover/src/styles.css
+++ b/discover/src/styles.css
@@ -527,6 +527,17 @@ h2 {
color: var(--youtube);
}
+a.badge {
+ display: inline-flex;
+ align-items: center;
+ gap: 4px;
+ text-decoration: none;
+}
+
+a.badge:hover {
+ background: color-mix(in srgb, currentColor 12%, transparent);
+}
+
.badge.library {
color: var(--library);
}
@@ -572,7 +583,8 @@ h2 {
text-align: center;
}
-.player-progress input {
+.player-progress input,
+.player-volume input {
flex: 1;
height: 4px;
appearance: none;
@@ -581,7 +593,8 @@ h2 {
cursor: pointer;
}
-.player-progress input::-webkit-slider-thumb {
+.player-progress input::-webkit-slider-thumb,
+.player-volume input::-webkit-slider-thumb {
appearance: none;
width: 12px;
height: 12px;
@@ -589,7 +602,8 @@ h2 {
background: var(--text);
}
-.player-progress input::-moz-range-thumb {
+.player-progress input::-moz-range-thumb,
+.player-volume input::-moz-range-thumb {
width: 12px;
height: 12px;
border: 0;
@@ -611,8 +625,21 @@ h2 {
color: var(--youtube);
}
-.player-queue {
+.player-side {
justify-self: end;
+ display: flex;
+ align-items: center;
+ gap: 16px;
+}
+
+.player-volume {
+ display: flex;
+ align-items: center;
+ gap: 6px;
+ width: 130px;
+}
+
+.player-queue {
color: var(--muted);
font-size: 0.85rem;
font-variant-numeric: tabular-nums;
@@ -1265,7 +1292,7 @@ h2 {
}
.player-progress,
- .player-queue,
+ .player-side,
.player .badge {
display: none;
}
diff --git a/discover/src/usePlayer.ts b/discover/src/usePlayer.ts
index 79875da..6708eea 100644
--- a/discover/src/usePlayer.ts
+++ b/discover/src/usePlayer.ts
@@ -7,6 +7,20 @@ export type PlayerStatus = "idle" | "loading" | "playing" | "paused" | "error";
export type Player = ReturnType;
+const VOLUME_KEY = "discover.volume";
+
+function savedVolume() {
+ try {
+ const v = Number(localStorage.getItem(VOLUME_KEY));
+ return localStorage.getItem(VOLUME_KEY) !== null && v >= 0 && v <= 1 ? v : 1;
+ } catch {
+ return 1;
+ }
+}
+
+/** The slider's position as a gain: squared, as loudness isn't heard linearly */
+const gain = (volume: number) => volume * volume;
+
/** The video being tried for the current YouTube item */
type YouTubeAttempt = {
suggestionId: string;
@@ -33,6 +47,10 @@ export function usePlayer(onError: (item: QueueItem) => void) {
const [status, setStatus] = useState("idle");
const [time, setTime] = useState(0);
const [duration, setDuration] = useState(0);
+ const [volume, setVolumeState] = useState(savedVolume);
+ const [muted, setMuted] = useState(false);
+ /** The YouTube video being played or tried for the current item */
+ const [videoId, setVideoId] = useState(null);
const current = index >= 0 ? queue[index] : undefined;
const onYouTube = current?.source === "youtube";
@@ -96,15 +114,47 @@ export function usePlayer(onError: (item: QueueItem) => void) {
else el.pause();
}, []);
+ // ---------- volume ----------
+
+ // what the players are set to, for a YouTube player made later
+ const output = useRef(0);
+ output.current = muted ? 0 : gain(volume);
+ useEffect(() => {
+ if (audio.current) audio.current.volume = output.current;
+ youtube.current?.setVolume(output.current);
+ }, [volume, muted]);
+
+ const setVolume = useCallback((v: number) => {
+ setVolumeState(v);
+ setMuted(false);
+ try {
+ localStorage.setItem(VOLUME_KEY, String(v));
+ } catch {
+ // only remembered for this visit
+ }
+ }, []);
+
+ const toggleMute = useCallback(() => setMuted((m) => !m), []);
+
// ---------- YouTube ----------
- const getYouTube = useCallback(
- () =>
- (youtube.current ??= new HiddenYouTubePlayer({
+ const getYouTube = useCallback(() => {
+ if (!youtube.current) {
+ youtube.current = new HiddenYouTubePlayer({
onState: (s) => youtubeEvents.current.onState(s),
onError: (code) => youtubeEvents.current.onError(code),
- })),
- [],
+ });
+ youtube.current.setVolume(output.current);
+ }
+ return youtube.current;
+ }, []);
+
+ const loadVideo = useCallback(
+ (id: string) => {
+ setVideoId(id);
+ return getYouTube().load(id);
+ },
+ [getYouTube],
);
/** Look up videos for a suggestion and play the first that works */
@@ -114,12 +164,12 @@ export function usePlayer(onError: (item: QueueItem) => void) {
const found = await api.youtubeVideos(suggestionId, refresh);
if (load !== loads.current) return;
attempt.current = { suggestionId, videoIds: found.video_ids, at: 0, cached: found.cached };
- await getYouTube().load(found.video_ids[0]);
+ await loadVideo(found.video_ids[0]);
} catch {
if (load === loads.current) failed();
}
},
- [failed, getYouTube],
+ [failed, loadVideo],
);
// the player's handlers, kept current for the player made on first use
@@ -146,7 +196,7 @@ export function usePlayer(onError: (item: QueueItem) => void) {
// usually a video that can't be embedded: try the next one
if (a.at + 1 < a.videoIds.length) {
attempt.current = { ...a, at: a.at + 1 };
- void getYouTube().load(a.videoIds[a.at + 1]).catch(failed);
+ void loadVideo(a.videoIds[a.at + 1]).catch(failed);
} else if (a.cached) {
attempt.current = null;
void playYouTube(a.suggestionId, loads.current, true);
@@ -177,6 +227,7 @@ export function usePlayer(onError: (item: QueueItem) => void) {
window.clearTimeout(errorTimer.current);
setTime(0);
setDuration(0);
+ setVideoId(null);
setStatus("loading");
if (current.source === "youtube" && current.suggestionId) {
el.pause();
@@ -257,5 +308,8 @@ export function usePlayer(onError: (item: QueueItem) => void) {
return () => window.removeEventListener("keydown", onKey);
}, [toggle]);
- return { queue, index, current, status, time, duration, playAt, next, previous, toggle, seek };
+ return {
+ queue, index, current, status, time, duration, volume, muted, videoId,
+ playAt, next, previous, toggle, seek, setVolume, toggleMute,
+ };
}
diff --git a/discover/src/youtubePlayer.ts b/discover/src/youtubePlayer.ts
index fbfe459..b9c7d05 100644
--- a/discover/src/youtubePlayer.ts
+++ b/discover/src/youtubePlayer.ts
@@ -8,6 +8,7 @@ type YTPlayer = {
pauseVideo(): void;
stopVideo(): void;
seekTo(seconds: number, allowSeekAhead: boolean): void;
+ setVolume(volume: number): void;
getCurrentTime(): number;
getDuration(): number;
getPlayerState(): number;
@@ -74,6 +75,8 @@ export class HiddenYouTubePlayer {
private ready: YTPlayer | null = null;
private host: HTMLElement | null = null;
private destroyed = false;
+ /** 0 to 100 */
+ private volumeLevel = 100;
constructor(private events: YouTubeEvents) {}
@@ -102,6 +105,7 @@ export class HiddenYouTubePlayer {
return;
}
this.ready = player;
+ player.setVolume(this.volumeLevel);
resolve(player);
},
onStateChange: (e) => this.events.onState(e.data),
@@ -135,6 +139,12 @@ export class HiddenYouTubePlayer {
this.ready?.seekTo(seconds, true);
}
+ /** 0 to 1 */
+ setVolume(volume: number) {
+ this.volumeLevel = Math.round(volume * 100);
+ this.ready?.setVolume(this.volumeLevel);
+ }
+
get playing() {
return this.ready?.getPlayerState() === State.Playing;
}