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

LB-1702: Added Volume Control Slider to BrainzPlayer #3097

Merged
merged 26 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
26 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
69852a8
Implemented suggested changes
Suvid-Singhal Jan 16, 2025
3963dfd
Fix linter issue
Suvid-Singhal Jan 16, 2025
7e20096
Merge branch 'master' into volume-slider
Suvid-Singhal Jan 16, 2025
30cbb37
Fixed CSS issues
Suvid-Singhal Jan 17, 2025
aaf2b6e
Merge branch 'master' into volume-slider
Suvid-Singhal Jan 17, 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
14 changes: 13 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 @@
} 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 @@ -470,6 +471,17 @@
if (!show) {
return null;
}
return <div>{this.getAlbumArt()}</div>;
return (
<BrainzPlayerContext.Consumer>
{(context) => {
const { volume } = context;
const player = this.appleMusicPlayer?.player;

Check failure on line 478 in frontend/js/src/common/brainzplayer/AppleMusicPlayer.tsx

View workflow job for this annotation

GitHub Actions / test

Property 'player' does not exist on type 'MusicKitInstance'. Did you mean 'play'?

Check failure on line 478 in frontend/js/src/common/brainzplayer/AppleMusicPlayer.tsx

View workflow job for this annotation

GitHub Actions / test

Property 'player' does not exist on type 'MusicKitInstance'. Did you mean 'play'?
if (player) {
player.volume = volume;
}
Suvid-Singhal marked this conversation as resolved.
Show resolved Hide resolved
return <div>{this.getAlbumArt()}</div>;
}}
</BrainzPlayerContext.Consumer>
);
}
}
1 change: 1 addition & 0 deletions frontend/js/src/common/brainzplayer/BrainzPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export default function BrainzPlayer() {
currentTrackURL,
playerPaused,
isActivated,
volume,
durationMs,
progressMs,
listenSubmitted,
Expand Down
7 changes: 7 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 All @@ -83,6 +86,7 @@ export type BrainzPlayerActionType = Partial<BrainzPlayerContextT> & {
| "ADD_LISTEN_TO_BOTTOM_OF_AMBIENT_QUEUE"
| "ADD_MULTIPLE_LISTEN_TO_BOTTOM_OF_AMBIENT_QUEUE";
data?: any;
payload?: any;
Suvid-Singhal marked this conversation as resolved.
Show resolved Hide resolved
};

function valueReducer(
Expand Down Expand Up @@ -179,6 +183,9 @@ function valueReducer(
currentListenIndex: newCurrentListenIndex,
};
}
case "VOLUME_CHANGE": {
return { ...state, volume: action.payload };
}
case "MOVE_AMBIENT_QUEUE_ITEM": {
const { ambientQueue } = state;
const evt = action.data as any;
Expand Down
30 changes: 30 additions & 0 deletions frontend/js/src/common/brainzplayer/BrainzPlayerUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
faBarsStaggered,
faFastBackward,
faFastForward,
faVolumeUp,
faHeart,
faHeartCrack,
faMusic,
Expand Down Expand Up @@ -75,6 +76,34 @@ function PlaybackControlButton(props: PlaybackControlButtonProps) {
);
}

function VolumeControlButton() {
Suvid-Singhal marked this conversation as resolved.
Show resolved Hide resolved
const { volume } = useBrainzPlayerContext();
const dispatch = useBrainzPlayerDispatch();
const volumeRef = React.useRef<HTMLInputElement>(null);
const handleVolumeChange = () => {
dispatch({
type: "VOLUME_CHANGE",
payload: volumeRef
? volumeRef.current?.value
? volumeRef.current.value
: 100
: 100,
});
Suvid-Singhal marked this conversation as resolved.
Show resolved Hide resolved
};
return (
<input
ref={volumeRef}
onMouseUp={handleVolumeChange}
className="volume"
type="range"
defaultValue="100"
max="100"
min="0"
step="5"
/>
);
Suvid-Singhal marked this conversation as resolved.
Show resolved Hide resolved
}

function BrainzPlayerUI(props: React.PropsWithChildren<BrainzPlayerUIProps>) {
const {
currentDataSourceName,
Expand Down Expand Up @@ -262,6 +291,7 @@ function BrainzPlayerUI(props: React.PropsWithChildren<BrainzPlayerUIProps>) {
disabled={disabled}
/>
</div>
<VolumeControlButton />
Suvid-Singhal marked this conversation as resolved.
Show resolved Hide resolved
<div className="actions">
{isPlayingATrack && currentDataSourceName && (
<a
Expand Down
45 changes: 27 additions & 18 deletions 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 @@ -406,24 +407,32 @@ export default class SoundcloudPlayer
render() {
const { show } = this.props;
return (
<div
className={`soundcloud ${!show ? "hidden" : ""}`}
data-testid={`soundcloud ${!show ? "hidden" : ""}`}
>
<iframe
id="soundcloud-iframe"
ref={this.iFrameRef}
title="Soundcloud player"
width="100%"
height="420px"
style={{ display: "none" }}
scrolling="no"
frameBorder="no"
allow="autoplay"
src="https://w.soundcloud.com/player/?auto_play=false"
/>
<div>{this.getAlbumArt()}</div>
</div>
<BrainzPlayerContext.Consumer>
{(context) => {
const { volume } = context;
this.soundcloudPlayer?.setVolume(volume / 100);
return (
<div
className={`soundcloud ${!show ? "hidden" : ""}`}
data-testid={`soundcloud ${!show ? "hidden" : ""}`}
>
<iframe
id="soundcloud-iframe"
ref={this.iFrameRef}
title="Soundcloud player"
width="100%"
height="420px"
style={{ display: "none" }}
scrolling="no"
frameBorder="no"
allow="autoplay"
src="https://w.soundcloud.com/player/?auto_play=false"
/>
<div>{this.getAlbumArt()}</div>
</div>
);
}}
</BrainzPlayerContext.Consumer>
);
}
}
12 changes: 10 additions & 2 deletions frontend/js/src/common/brainzplayer/SpotifyPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
getArtistName,
} from "../../utils/utils";
import { DataSourceType, DataSourceProps } from "./BrainzPlayer";
import { BrainzPlayerContext } from "./BrainzPlayerContext";
import GlobalAppContext from "../../utils/GlobalAppContext";
import { dataSourcesInfo } from "../../settings/brainzplayer/BrainzPlayerSettings";

Expand Down Expand Up @@ -515,7 +516,6 @@ export default class SpotifyPlayer
duration,
track_window: { current_track, previous_tracks },
} = playerState;

const { currentSpotifyTrack, durationMs } = this.state;
const { playerPaused } = this.props;
const {
Expand Down Expand Up @@ -617,6 +617,14 @@ export default class SpotifyPlayer
if (!show) {
return null;
}
return <div data-testid="spotify-player">{this.getAlbumArt()}</div>;
return (
<BrainzPlayerContext.Consumer>
{(context) => {
const { volume } = context;
this.spotifyPlayer?.setVolume(volume / 100);
return <div data-testid="spotify-player">{this.getAlbumArt()}</div>;
}}
</BrainzPlayerContext.Consumer>
);
Suvid-Singhal marked this conversation as resolved.
Show resolved Hide resolved
}
anshg1214 marked this conversation as resolved.
Show resolved Hide resolved
}
58 changes: 35 additions & 23 deletions 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 @@ -376,30 +377,41 @@ export default class YoutubePlayer extends React.Component<YoutubePlayerProps>
const leftBound =
document.body.clientWidth - draggableBoundPadding * 2 - 350;
return (
<Draggable
handle=".youtube-drag-handle"
bounds={{
left: -leftBound,
right: -draggableBoundPadding,
bottom: -draggableBoundPadding,
<BrainzPlayerContext.Consumer>
{(context) => {
const { volume } = context;
this.youtubePlayer?.setVolume(volume);
return (
<Draggable
handle=".youtube-drag-handle"
bounds={{
left: -leftBound,
right: -draggableBoundPadding,
bottom: -draggableBoundPadding,
}}
>
<div
className={`youtube-wrapper${!show ? " hidden" : ""}`}
data-testid={`youtube-wrapper${!show ? " hidden" : ""}`}
>
<button
className="btn btn-sm youtube-drag-handle"
type="button"
>
<FontAwesomeIcon icon={faArrowsAlt} />
</button>
<YouTube
className="youtube-player"
opts={options}
onError={this.onError}
onStateChange={this.handlePlayerStateChanged}
onReady={this.onReady}
/>
</div>
</Draggable>
);
}}
>
<div
className={`youtube-wrapper${!show ? " hidden" : ""}`}
data-testid={`youtube-wrapper${!show ? " hidden" : ""}`}
>
<button className="btn btn-sm youtube-drag-handle" type="button">
<FontAwesomeIcon icon={faArrowsAlt} />
</button>
<YouTube
className="youtube-player"
opts={options}
onError={this.onError}
onStateChange={this.handlePlayerStateChanged}
onReady={this.onReady}
/>
</div>
</Draggable>
</BrainzPlayerContext.Consumer>
);
}
}
Loading