Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Added Volume Control Slider to BrainzPlayer #3097

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
a1cd0c4
Added Volume Control Slider to BrainzPlayer
Suvid-Singhal Dec 27, 2024
f753ce3
Removed debug console.log() statements
Suvid-Singhal Dec 27, 2024
86876e4
Merge branch 'master' into volume-slider
Suvid-Singhal Jan 3, 2025
49e3cc3
Lint css files
Suvid-Singhal Jan 3, 2025
e21d449
Fixed some errors
Suvid-Singhal Jan 7, 2025
602ecaa
Fix some TS errors
Suvid-Singhal Jan 8, 2025
0cc8fe3
Merge branch 'master' into volume-slider
Suvid-Singhal Jan 8, 2025
c9b2d31
Better coding practices and implemented few suggested changes
Suvid-Singhal Jan 8, 2025
f3ce046
Merge branch 'master' into volume-slider
Suvid-Singhal Jan 8, 2025
db6dcfe
Fixed test files errors
Suvid-Singhal Jan 8, 2025
ab8fd77
Moved VolumeControlButton to a new file and set default volume
Suvid-Singhal Jan 9, 2025
8f3ab8b
Merge branch 'master' into volume-slider
Suvid-Singhal Jan 9, 2025
b7b0423
Hide volume slider for small and extra small viewports
Suvid-Singhal Jan 12, 2025
4b18996
Merge branch 'master' into volume-slider
Suvid-Singhal Jan 12, 2025
89b67bd
Changed few css classes and code refactoring
Suvid-Singhal Jan 14, 2025
f156734
Merge branch 'master' into volume-slider
Suvid-Singhal Jan 14, 2025
c851b1e
Removed setVolume from render function in all the players and fixed s…
Suvid-Singhal Jan 14, 2025
7fd2c7d
Merge branch 'master' into volume-slider
Suvid-Singhal Jan 15, 2025
1faccbe
Removed unused elemts from props, fixed linting errors
Suvid-Singhal Jan 15, 2025
d551af7
Added button to reveal volume slider and refactored code
Suvid-Singhal Jan 16, 2025
890daf7
Fix a small error
Suvid-Singhal Jan 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions frontend/css/brainzplayer.less
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,23 @@
}
}

.volume {
position: fixed;
width: fit-content;
z-index: 109;
height: 10em;
background-color: #f8f8f8;
right: 16em;
.volume-input {
width: 7em;
margin-top: @brainzplayer-height;
transform: rotate(270deg);
}
&.show {
bottom: @brainzplayer-height;
}
}

.queue {
position: fixed;
padding: 1em;
Expand Down
7 changes: 6 additions & 1 deletion frontend/js/src/common/brainzplayer/AppleMusicPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "../../utils/utils";
import { DataSourceProps, DataSourceType } from "./BrainzPlayer";
import GlobalAppContext from "../../utils/GlobalAppContext";
import { BrainzPlayerContext } from "./BrainzPlayerContext";
import { dataSourcesInfo } from "../../settings/brainzplayer/BrainzPlayerSettings";

export type AppleMusicPlayerProps = DataSourceProps;
Expand Down Expand Up @@ -148,7 +149,11 @@ export default class AppleMusicPlayer
}

componentDidUpdate(prevProps: DataSourceProps) {
const { show } = this.props;
const { show, volume } = this.props;
const player = this.appleMusicPlayer;
if (prevProps.volume !== volume && player) {
player.volume = (volume ?? 100) / 100;
}
if (prevProps.show && !show) {
this.stopAndClear();
}
Expand Down
6 changes: 6 additions & 0 deletions frontend/js/src/common/brainzplayer/BrainzPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export type DataSourceTypes =

export type DataSourceProps = {
show: boolean;
volume?: number;
playerPaused: boolean;
onPlayerPausedChange: (paused: boolean) => void;
onProgressChange: (progressMs: number) => void;
Expand Down Expand Up @@ -153,6 +154,7 @@ export default function BrainzPlayer() {
currentTrackURL,
playerPaused,
isActivated,
volume,
durationMs,
progressMs,
listenSubmitted,
Expand Down Expand Up @@ -1023,6 +1025,7 @@ export default function BrainzPlayer() {
>
{userPreferences?.brainzplayer?.spotifyEnabled !== false && (
<SpotifyPlayer
volume={volume}
show={
isActivated &&
dataSourceRefs[currentDataSourceIndex]?.current instanceof
Expand All @@ -1045,6 +1048,7 @@ export default function BrainzPlayer() {
)}
{userPreferences?.brainzplayer?.youtubeEnabled !== false && (
<YoutubePlayer
volume={volume}
show={
isActivated &&
dataSourceRefs[currentDataSourceIndex]?.current instanceof
Expand All @@ -1068,6 +1072,7 @@ export default function BrainzPlayer() {
)}
{userPreferences?.brainzplayer?.soundcloudEnabled !== false && (
<SoundcloudPlayer
volume={volume}
show={
isActivated &&
dataSourceRefs[currentDataSourceIndex]?.current instanceof
Expand All @@ -1090,6 +1095,7 @@ export default function BrainzPlayer() {
)}
{userPreferences?.brainzplayer?.appleMusicEnabled !== false && (
<AppleMusicPlayer
volume={volume}
show={
isActivated &&
dataSourceRefs[currentDataSourceIndex]?.current instanceof
Expand Down
6 changes: 6 additions & 0 deletions frontend/js/src/common/brainzplayer/BrainzPlayerContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type BrainzPlayerContextT = {
currentTrackURL?: string;
playerPaused: boolean;
isActivated: boolean;
volume: number;
durationMs: number;
progressMs: number;
updateTime: number;
Expand All @@ -57,6 +58,7 @@ export const initialValue: BrainzPlayerContextT = {
currentTrackArtist: "",
playerPaused: true,
isActivated: false,
volume: 100,
durationMs: 0,
progressMs: 0,
updateTime: performance.now(),
Expand All @@ -73,6 +75,7 @@ export type BrainzPlayerActionType = Partial<BrainzPlayerContextT> & {
| "SET_PLAYBACK_TIMER"
| "TOGGLE_REPEAT_MODE"
| "MOVE_QUEUE_ITEM"
| "VOLUME_CHANGE"
| "CLEAR_QUEUE_AFTER_CURRENT_AND_SET_AMBIENT_QUEUE"
| "MOVE_AMBIENT_QUEUE_ITEM"
| "MOVE_AMBIENT_QUEUE_ITEMS_TO_QUEUE"
Expand Down Expand Up @@ -179,6 +182,9 @@ function valueReducer(
currentListenIndex: newCurrentListenIndex,
};
}
case "VOLUME_CHANGE": {
return { ...state, volume: action.data };
}
case "MOVE_AMBIENT_QUEUE_ITEM": {
const { ambientQueue } = state;
const evt = action.data as any;
Expand Down
40 changes: 28 additions & 12 deletions frontend/js/src/common/brainzplayer/BrainzPlayerUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
faPauseCircle,
faPlayCircle,
faSlash,
faVolumeUp,
} from "@fortawesome/free-solid-svg-icons";
import * as React from "react";

Expand All @@ -29,6 +30,7 @@ import {
useBrainzPlayerDispatch,
} from "./BrainzPlayerContext";
import Queue from "./Queue";
import VolumeControlButton from "./VolumeControlButton";

type BrainzPlayerUIProps = {
currentDataSourceName?: string;
Expand Down Expand Up @@ -88,7 +90,7 @@ function BrainzPlayerUI(props: React.PropsWithChildren<BrainzPlayerUIProps>) {
const { currentUser } = React.useContext(GlobalAppContext);

// BrainzPlayerContext
const { queueRepeatMode } = useBrainzPlayerContext();
const { queueRepeatMode, volume } = useBrainzPlayerContext();
const dispatch = useBrainzPlayerDispatch();

// const { currentListenFeedback } = this.state;
Expand Down Expand Up @@ -191,13 +193,18 @@ function BrainzPlayerUI(props: React.PropsWithChildren<BrainzPlayerUIProps>) {
const playbackDisabledText = "Playback disabled in preferences";

const [showQueue, setShowQueue] = React.useState(false);
const [showVolume, setShowVolume] = React.useState(false);

const toggleRepeatMode = () => {
dispatch({ type: "TOGGLE_REPEAT_MODE" });
};

return (
<>
<div className={`volume ${showVolume ? "show" : ""}`}>
<VolumeControlButton />
</div>

<div className={`queue ${showQueue ? "show" : ""}`}>
<Queue clearQueue={clearQueue} onHide={() => setShowQueue(false)} />
</div>
Expand Down Expand Up @@ -264,20 +271,29 @@ function BrainzPlayerUI(props: React.PropsWithChildren<BrainzPlayerUIProps>) {
</div>
<div className="actions">
{isPlayingATrack && currentDataSourceName && (
<a
href={trackUrl || "#"}
className="music-service-icon"
aria-label={`Open in ${currentDataSourceName}`}
title={`Open in ${currentDataSourceName}`}
target="_blank"
rel="noopener noreferrer"
>
<>
<a
href={trackUrl || "#"}
className="music-service-icon"
aria-label={`Open in ${currentDataSourceName}`}
title={`Open in ${currentDataSourceName}`}
target="_blank"
rel="noopener noreferrer"
>
<FontAwesomeIcon
icon={currentDataSourceIcon!}
color={currentDataSourceIconColor}
/>
</a>
<FontAwesomeIcon
icon={currentDataSourceIcon!}
color={currentDataSourceIconColor}
icon={faVolumeUp}
style={{ color: showVolume ? "green" : "" }}
onClick={() => setShowVolume(!showVolume)}
className="hidden-sm hidden-xs"
/>
</a>
</>
)}

<FontAwesomeIcon
icon={faBarsStaggered}
style={{ color: showQueue ? "green" : "" }}
Expand Down
8 changes: 7 additions & 1 deletion frontend/js/src/common/brainzplayer/SoundcloudPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
searchForSoundcloudTrack,
} from "../../utils/utils";
import GlobalAppContext from "../../utils/GlobalAppContext";
import { BrainzPlayerContext } from "./BrainzPlayerContext";
import { dataSourcesInfo } from "../../settings/brainzplayer/BrainzPlayerSettings";

require("../../../lib/soundcloud-player-api");
Expand Down Expand Up @@ -152,7 +153,11 @@ export default class SoundcloudPlayer
}

componentDidUpdate(prevProps: DataSourceProps) {
const { show } = this.props;
const { show, volume } = this.props;
if (prevProps.volume !== volume && this.soundcloudPlayer?.setVolume) {
this.soundcloudPlayer?.setVolume((volume ?? 100) / 100);
}

if (prevProps.show && !show && this.soundcloudPlayer) {
this.soundcloudPlayer.pause();
}
Expand Down Expand Up @@ -405,6 +410,7 @@ export default class SoundcloudPlayer

render() {
const { show } = this.props;

return (
<div
className={`soundcloud ${!show ? "hidden" : ""}`}
Expand Down
14 changes: 9 additions & 5 deletions frontend/js/src/common/brainzplayer/SpotifyPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ export default class SpotifyPlayer
}

componentDidUpdate(prevProps: DataSourceProps) {
const { show } = this.props;
const { show, volume } = this.props;
if (prevProps.volume !== volume && this.spotifyPlayer?.setVolume) {
this.spotifyPlayer?.setVolume((volume ?? 100) / 100);
}

if (prevProps.show === true && show === false) {
this.stopAndClear();
}
Expand Down Expand Up @@ -436,7 +440,7 @@ export default class SpotifyPlayer
);
return;
}
const { refreshSpotifyToken } = this.props;
const { refreshSpotifyToken, volume } = this.props;
const { spotifyAuth: spotifyUser = undefined } = this.context;

this.spotifyPlayer = new window.Spotify.Player({
Expand All @@ -462,7 +466,7 @@ export default class SpotifyPlayer
);
}
},
volume: 0.7, // Careful with this, now…
volume: volume ?? 100 / 100,
});

// Error handling
Expand Down Expand Up @@ -515,9 +519,9 @@ export default class SpotifyPlayer
duration,
track_window: { current_track, previous_tracks },
} = playerState;

const { currentSpotifyTrack, durationMs } = this.state;
const { playerPaused } = this.props;
const { playerPaused, volume } = this.props;
this.spotifyPlayer?.setVolume((volume ?? 100) / 100);
const {
onPlayerPausedChange,
onProgressChange,
Expand Down
25 changes: 25 additions & 0 deletions frontend/js/src/common/brainzplayer/VolumeControlButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as React from "react";
import { useBrainzPlayerDispatch } from "./BrainzPlayerContext";

function VolumeControlButton() {
const dispatch = useBrainzPlayerDispatch();
const handleVolumeChange = (e: React.MouseEvent<HTMLInputElement>) => {
dispatch({
type: "VOLUME_CHANGE",
data: e.currentTarget?.value ?? 100,
});
};
return (
<input
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of rendering the input directly in here, I suggest rendering a volume icon. When clicking on the icon, the volume input will appear above it.
For CSS styles for the hidden/shown input, you can take inspiration from the player queue (the three horizontal bars in the player UI)
https://github.com/metabrainz/listenbrainz-server/blob/f40c908885229d42c5cb7716ea57a54d39ed2ca4/frontend/css/brainzplayer.less#L354C2-L370C4

onMouseUp={handleVolumeChange}
className="volume-input"
type="range"
defaultValue="100"
max="100"
min="0"
step="5"
/>
);
}

export default VolumeControlButton;
7 changes: 6 additions & 1 deletion frontend/js/src/common/brainzplayer/YoutubePlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
searchForYoutubeTrack,
} from "../../utils/utils";
import { DataSourceProps, DataSourceType } from "./BrainzPlayer";
import { BrainzPlayerContext } from "./BrainzPlayerContext";
import { dataSourcesInfo } from "../../settings/brainzplayer/BrainzPlayerSettings";

export type YoutubePlayerProps = DataSourceProps & {
Expand Down Expand Up @@ -118,7 +119,10 @@ export default class YoutubePlayer extends React.Component<YoutubePlayerProps>
checkVideoLoadedTimerId?: NodeJS.Timeout;

componentDidUpdate(prevProps: DataSourceProps) {
const { show } = this.props;
const { show, volume } = this.props;
if (prevProps.volume !== volume && this.youtubePlayer?.setVolume) {
this.youtubePlayer?.setVolume(volume ?? 100);
}
if (prevProps.show && !show && this.youtubePlayer) {
this.youtubePlayer.stopVideo();
// Clear playlist
Expand Down Expand Up @@ -375,6 +379,7 @@ export default class YoutubePlayer extends React.Component<YoutubePlayerProps>
// width of screen - padding on each side - youtube player width
const leftBound =
document.body.clientWidth - draggableBoundPadding * 2 - 350;

return (
<Draggable
handle=".youtube-drag-handle"
Expand Down
1 change: 1 addition & 0 deletions frontend/js/src/utils/musickit.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ declare namespace MusicKit {
setQueue(options: SetQueueOptions): Promise<any>;

stop(): void;
volume: number;
}

declare type NowPlayingItem = {
Expand Down
Loading