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";
|
import * as Icon from "./icons";
|
||||||
|
|
||||||
export function PlayerBar({ player }: { player: Player }) {
|
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;
|
if (!current) return null;
|
||||||
const label =
|
const label =
|
||||||
status === "loading"
|
status === "loading"
|
||||||
@@ -18,9 +18,21 @@ export function PlayerBar({ player }: { player: Player }) {
|
|||||||
<div className="track-title">{current.title}</div>
|
<div className="track-title">{current.title}</div>
|
||||||
<div className="track-sub">{current.artist}</div>
|
<div className="track-sub">{current.artist}</div>
|
||||||
</div>
|
</div>
|
||||||
<span className={`badge ${current.source}`}>
|
{current.source === "youtube" && videoId ? (
|
||||||
{current.source === "youtube" ? "YouTube" : "Library"}
|
<a
|
||||||
</span>
|
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>
|
||||||
<div className="player-main">
|
<div className="player-main">
|
||||||
<div className="player-buttons">
|
<div className="player-buttons">
|
||||||
@@ -47,8 +59,25 @@ export function PlayerBar({ player }: { player: Player }) {
|
|||||||
</div>
|
</div>
|
||||||
{label && <div className={`player-status ${status}`}>{label}</div>}
|
{label && <div className={`player-status ${status}`}>{label}</div>}
|
||||||
</div>
|
</div>
|
||||||
<div className="player-queue">
|
<div className="player-side">
|
||||||
{player.index + 1} / {player.queue.length}
|
<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>
|
</div>
|
||||||
</footer>
|
</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 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 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 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);
|
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 {
|
.badge.library {
|
||||||
color: var(--library);
|
color: var(--library);
|
||||||
}
|
}
|
||||||
@@ -572,7 +583,8 @@ h2 {
|
|||||||
text-align: center;
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
.player-progress input {
|
.player-progress input,
|
||||||
|
.player-volume input {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
height: 4px;
|
height: 4px;
|
||||||
appearance: none;
|
appearance: none;
|
||||||
@@ -581,7 +593,8 @@ h2 {
|
|||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
.player-progress input::-webkit-slider-thumb {
|
.player-progress input::-webkit-slider-thumb,
|
||||||
|
.player-volume input::-webkit-slider-thumb {
|
||||||
appearance: none;
|
appearance: none;
|
||||||
width: 12px;
|
width: 12px;
|
||||||
height: 12px;
|
height: 12px;
|
||||||
@@ -589,7 +602,8 @@ h2 {
|
|||||||
background: var(--text);
|
background: var(--text);
|
||||||
}
|
}
|
||||||
|
|
||||||
.player-progress input::-moz-range-thumb {
|
.player-progress input::-moz-range-thumb,
|
||||||
|
.player-volume input::-moz-range-thumb {
|
||||||
width: 12px;
|
width: 12px;
|
||||||
height: 12px;
|
height: 12px;
|
||||||
border: 0;
|
border: 0;
|
||||||
@@ -611,8 +625,21 @@ h2 {
|
|||||||
color: var(--youtube);
|
color: var(--youtube);
|
||||||
}
|
}
|
||||||
|
|
||||||
.player-queue {
|
.player-side {
|
||||||
justify-self: end;
|
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);
|
color: var(--muted);
|
||||||
font-size: 0.85rem;
|
font-size: 0.85rem;
|
||||||
font-variant-numeric: tabular-nums;
|
font-variant-numeric: tabular-nums;
|
||||||
@@ -1265,7 +1292,7 @@ h2 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.player-progress,
|
.player-progress,
|
||||||
.player-queue,
|
.player-side,
|
||||||
.player .badge {
|
.player .badge {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,20 @@ export type PlayerStatus = "idle" | "loading" | "playing" | "paused" | "error";
|
|||||||
|
|
||||||
export type Player = ReturnType<typeof usePlayer>;
|
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 */
|
/** The video being tried for the current YouTube item */
|
||||||
type YouTubeAttempt = {
|
type YouTubeAttempt = {
|
||||||
suggestionId: string;
|
suggestionId: string;
|
||||||
@@ -33,6 +47,10 @@ export function usePlayer(onError: (item: QueueItem) => void) {
|
|||||||
const [status, setStatus] = useState<PlayerStatus>("idle");
|
const [status, setStatus] = useState<PlayerStatus>("idle");
|
||||||
const [time, setTime] = useState(0);
|
const [time, setTime] = useState(0);
|
||||||
const [duration, setDuration] = 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 current = index >= 0 ? queue[index] : undefined;
|
||||||
const onYouTube = current?.source === "youtube";
|
const onYouTube = current?.source === "youtube";
|
||||||
|
|
||||||
@@ -96,15 +114,47 @@ export function usePlayer(onError: (item: QueueItem) => void) {
|
|||||||
else el.pause();
|
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 ----------
|
// ---------- YouTube ----------
|
||||||
|
|
||||||
const getYouTube = useCallback(
|
const getYouTube = useCallback(() => {
|
||||||
() =>
|
if (!youtube.current) {
|
||||||
(youtube.current ??= new HiddenYouTubePlayer({
|
youtube.current = new HiddenYouTubePlayer({
|
||||||
onState: (s) => youtubeEvents.current.onState(s),
|
onState: (s) => youtubeEvents.current.onState(s),
|
||||||
onError: (code) => youtubeEvents.current.onError(code),
|
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 */
|
/** 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);
|
const found = await api.youtubeVideos(suggestionId, refresh);
|
||||||
if (load !== loads.current) return;
|
if (load !== loads.current) return;
|
||||||
attempt.current = { suggestionId, videoIds: found.video_ids, at: 0, cached: found.cached };
|
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 {
|
} catch {
|
||||||
if (load === loads.current) failed();
|
if (load === loads.current) failed();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[failed, getYouTube],
|
[failed, loadVideo],
|
||||||
);
|
);
|
||||||
|
|
||||||
// the player's handlers, kept current for the player made on first use
|
// 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
|
// usually a video that can't be embedded: try the next one
|
||||||
if (a.at + 1 < a.videoIds.length) {
|
if (a.at + 1 < a.videoIds.length) {
|
||||||
attempt.current = { ...a, at: a.at + 1 };
|
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) {
|
} else if (a.cached) {
|
||||||
attempt.current = null;
|
attempt.current = null;
|
||||||
void playYouTube(a.suggestionId, loads.current, true);
|
void playYouTube(a.suggestionId, loads.current, true);
|
||||||
@@ -177,6 +227,7 @@ export function usePlayer(onError: (item: QueueItem) => void) {
|
|||||||
window.clearTimeout(errorTimer.current);
|
window.clearTimeout(errorTimer.current);
|
||||||
setTime(0);
|
setTime(0);
|
||||||
setDuration(0);
|
setDuration(0);
|
||||||
|
setVideoId(null);
|
||||||
setStatus("loading");
|
setStatus("loading");
|
||||||
if (current.source === "youtube" && current.suggestionId) {
|
if (current.source === "youtube" && current.suggestionId) {
|
||||||
el.pause();
|
el.pause();
|
||||||
@@ -257,5 +308,8 @@ export function usePlayer(onError: (item: QueueItem) => void) {
|
|||||||
return () => window.removeEventListener("keydown", onKey);
|
return () => window.removeEventListener("keydown", onKey);
|
||||||
}, [toggle]);
|
}, [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;
|
pauseVideo(): void;
|
||||||
stopVideo(): void;
|
stopVideo(): void;
|
||||||
seekTo(seconds: number, allowSeekAhead: boolean): void;
|
seekTo(seconds: number, allowSeekAhead: boolean): void;
|
||||||
|
setVolume(volume: number): void;
|
||||||
getCurrentTime(): number;
|
getCurrentTime(): number;
|
||||||
getDuration(): number;
|
getDuration(): number;
|
||||||
getPlayerState(): number;
|
getPlayerState(): number;
|
||||||
@@ -74,6 +75,8 @@ export class HiddenYouTubePlayer {
|
|||||||
private ready: YTPlayer | null = null;
|
private ready: YTPlayer | null = null;
|
||||||
private host: HTMLElement | null = null;
|
private host: HTMLElement | null = null;
|
||||||
private destroyed = false;
|
private destroyed = false;
|
||||||
|
/** 0 to 100 */
|
||||||
|
private volumeLevel = 100;
|
||||||
|
|
||||||
constructor(private events: YouTubeEvents) {}
|
constructor(private events: YouTubeEvents) {}
|
||||||
|
|
||||||
@@ -102,6 +105,7 @@ export class HiddenYouTubePlayer {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.ready = player;
|
this.ready = player;
|
||||||
|
player.setVolume(this.volumeLevel);
|
||||||
resolve(player);
|
resolve(player);
|
||||||
},
|
},
|
||||||
onStateChange: (e) => this.events.onState(e.data),
|
onStateChange: (e) => this.events.onState(e.data),
|
||||||
@@ -135,6 +139,12 @@ export class HiddenYouTubePlayer {
|
|||||||
this.ready?.seekTo(seconds, true);
|
this.ready?.seekTo(seconds, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 0 to 1 */
|
||||||
|
setVolume(volume: number) {
|
||||||
|
this.volumeLevel = Math.round(volume * 100);
|
||||||
|
this.ready?.setVolume(this.volumeLevel);
|
||||||
|
}
|
||||||
|
|
||||||
get playing() {
|
get playing() {
|
||||||
return this.ready?.getPlayerState() === State.Playing;
|
return this.ready?.getPlayerState() === State.Playing;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user