Skip to content

feat: Allow volume control of Music & Sound with property#1311

Open
DeathPhoenix22 wants to merge 1 commit intoAlmasB:devfrom
DeathPhoenix22:#1310
Open

feat: Allow volume control of Music & Sound with property#1311
DeathPhoenix22 wants to merge 1 commit intoAlmasB:devfrom
DeathPhoenix22:#1310

Conversation

@DeathPhoenix22
Copy link
Copy Markdown
Contributor

@DeathPhoenix22 DeathPhoenix22 commented Oct 24, 2023

Hi,

Issue Link: #1310

Here's an implementation that allow custom Volume configuration of all Sounds & Musics. I've placed the "Master / Global" volume property of the AudioType inside them. This felt pretty natural since the "expert" (GRAPS) of that information is the AudioType itself. This also mean that we could really easily add a new AudioType without much efforts and that new type would automatically mix itself against the Global and the current Audio.

@AlmasB NOTE: I took the initiative to revert the changes made in #1187. Granting access to the internal audio is causing a bug related to volume adjustments in the settings. Whenever a player was adjusting his Global volume, all custom changes made on the audio (getAudio()) was automatically reverted. With this implementation, the conjunction of the 2 values are always kept (via the mix() method)

Example:

Music music = getAssetLoader().loadMusic("rain.mp3");
music.setVolume(0.0);
Timeline timeline = new Timeline(
    new KeyFrame(Duration.seconds(5),
    new KeyValue(music.volumeProperty(), 1.0))
);
FXGL.getAudioPlayer().playMusic(music);
timeline.play();

Note that changing the Global Music volume will also automatically impact all Music:

AudioType.MUSIC.getVolume().set(AudioType.MUSIC.getVolume().get() - 0.1);

I've tested it on my own game and I needed it!
I hope you like it! :)


private val loader = DesktopAndMobileAudioLoader()

override fun onMainLoopStarting() {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Automatically handled by the Audio with Property change listeners

if (!activeSounds.containsByIdentity(sound))
activeSounds.add(sound)

sound.audio.setVolume(soundVolume.value)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No need anymore, the global volumes are automatically mixed with the Media configured sound

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant