Volume slider
This commit is contained in:
1 parent
c84e7e715d
commit
68a6a7523c
5 files changed
+142
-20
No files matched your search
@@ -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 }) {
|
||||
<div className="track-title">{current.title}</div>
|
||||
<div className="track-sub">{current.artist}</div>
|
||||
</div>
|
||||
<span className={`badge ${current.source}`}>
|
||||
{current.source === "youtube" ? "YouTube" : "Library"}
|
||||
</span>
|
||||
{current.source === "youtube" && videoId ? (
|
||||
<a
|
||||
className="badge youtube"
|
||||
href={`https://www.youtube.com/watch?v=${encodeURIComponent(videoId)}`}
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
title="Open on YouTube"
|
||||
>
|
||||
YouTube <Icon.External size={11} />
|
||||
</a>
|
||||
) : (
|
||||
<span className={`badge ${current.source}`}>
|
||||
{current.source === "youtube" ? "YouTube" : "Library"}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="player-main">
|
||||
<div className="player-buttons">
|
||||
@@ -47,8 +59,25 @@ export function PlayerBar({ player }: { player: Player }) {
|
||||
</div>
|
||||
{label && <div className={`player-status ${status}`}>{label}</div>}
|
||||
</div>
|
||||
<div className="player-queue">
|
||||
{player.index + 1} / {player.queue.length}
|
||||
<div className="player-side">
|
||||
<div className="player-volume">
|
||||
<button className="icon-button small" onClick={player.toggleMute} aria-label={muted ? "Unmute" : "Mute"}>
|
||||
{muted || volume === 0 ? <Icon.Muted /> : <Icon.Volume />}
|
||||
</button>
|
||||
<input
|
||||
type="range"
|
||||
min={0}
|
||||
max={1}
|
||||
step={0.01}
|
||||
value={muted ? 0 : volume}
|
||||
onChange={(e) => player.setVolume(Number(e.target.value))}
|
||||
aria-label="Volume"
|
||||
style={{ ["--progress" as string]: `${(muted ? 0 : volume) * 100}%` }}
|
||||
/>
|
||||
</div>
|
||||
<div className="player-queue">
|
||||
{player.index + 1} / {player.queue.length}
|
||||
</div>
|
||||
</div>
|
||||
</footer>
|
||||
);
|
||||
|
||||
@@ -21,3 +21,5 @@ export const ListMusic = ({ size }: P) => svg(<><path d="M4 6h11M4 11h11M4 16h7"
|
||||
export const Calendar = ({ size }: P) => svg(<><rect x="4" y="5" width="16" height="15" rx="2" /><path d="M4 10h16M9 3v4M15 3v4" /></>, size);
|
||||
export const Warning = ({ size }: P) => svg(<><path d="M12 4l9 16H3z" /><path d="M12 10v4M12 17v.5" /></>, size);
|
||||
export const User = ({ size }: P) => svg(<><circle cx="12" cy="8" r="4" /><path d="M4 20c1.5-3.5 4.5-5 8-5s6.5 1.5 8 5" /></>, size);
|
||||
export const Volume = ({ size }: P) => svg(<><path d="M4 9.5h3.5L12 5.5v13l-4.5-4H4z" fill="currentColor" stroke="none" /><path d="M15.5 9a4 4 0 0 1 0 6" /><path d="M18 6.5a7.5 7.5 0 0 1 0 11" /></>, size);
|
||||
export const Muted = ({ size }: P) => svg(<><path d="M4 9.5h3.5L12 5.5v13l-4.5-4H4z" fill="currentColor" stroke="none" /><path d="M16 9.5l5 5M21 9.5l-5 5" /></>, size);
|
||||
+32
-5
@@ -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;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,20 @@ export type PlayerStatus = "idle" | "loading" | "playing" | "paused" | "error";
|
||||
|
||||
export type Player = ReturnType<typeof usePlayer>;
|
||||
|
||||
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<PlayerStatus>("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<string | null>(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,
|
||||
};
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user